小编Dav*_*ard的帖子

当您使用 '++' 递增 std::vector<int>::iterator 时,它的类型是否会改变?

我正在尝试 C++20 概念,但遇到了一个无法解释的奇怪问题。

\n

我正在尝试定义一个concept检查给定迭代器是否可以递增(很好)的方法,并且还检查类型是否因此而改变(不好)。

\n

原谅我的无知,我错过了什么?错误的最后一行指出:

\n

\' 注意:\xe2\x80\x98++ it\xe2\x80\x99 不满足返回类型要求\n{ ++it } -> std::same_as; \'

\n
#include <functional>\n#include <vector>\n\ntemplate<typename Iter>\nconcept Iterable = requires(Iter it){\n    { ++it } // Check iterator can be incremented (compiles but does not check return type)\n    { ++it } -> std::same_as<Iter>; // Also check the return type (**fails**)\n};\n\ntemplate<typename Iter>\nrequires Iterable<Iter>\nvoid doesNothing(Iter current, Iter end){\n    while(current != end){\n        ++current;\n    }\n};\n\nint main(){\n    std::vector<int> v = {1, 2, 3};\n    doesNothing(v.begin(), v.end());\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

预先非常感谢您。 …

c++ stdvector c++-concepts c++20

6
推荐指数
1
解决办法
141
查看次数

标签 统计

c++ ×1

c++-concepts ×1

c++20 ×1

stdvector ×1