我正在尝试 C++20 概念,但遇到了一个无法解释的奇怪问题。
\n我正在尝试定义一个concept检查给定迭代器是否可以递增(很好)的方法,并且还检查类型是否因此而改变(不好)。
原谅我的无知,我错过了什么?错误的最后一行指出:
\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}\nRun Code Online (Sandbox Code Playgroud)\n预先非常感谢您。 …