std::indirectly_unary_invocable,std::indirectly_regular_unary_invocable
定义于头文件 <iterator> |
||
std::indirectly_unary_invocable |
||
template< class F, class I > concept indirectly_unary_invocable = |
(自 C++20 起) | |
std::indirectly_regular_unary_invocable |
||
template< class F, class I > concept indirectly_regular_unary_invocable = |
(自 C++20 起) | |
概念 indirectly_unary_invocable
和 indirectly_regular_unary_invocable
为算法指定了需求,这些算法调用(通常的)一元可调用对象作为其参数。 这些概念与 std::invocable 之间的主要区别在于,它们应用于 I
引用的类型,而不是 I
本身。
[编辑] 注解
indirectly_unary_invocable
和 indirectly_regular_unary_invocable
之间的区别纯粹是语义上的。
[编辑] 示例
#include <algorithm> #include <iterator> #include <print> #include <ranges> struct IntWrapper { int i; explicit IntWrapper(int i) : i(i) {} IntWrapper(IntWrapper&&) = default; IntWrapper& operator=(IntWrapper&&) = default; }; int main() { auto ints = std::views::iota(1, 10); auto print = [] (IntWrapper w) { std::print("{} ", w.i); }; auto wrap = [] (int i) { return IntWrapper{i}; }; using Proj = std::projected<decltype(ints.begin()), decltype(wrap)>; // error (evaluated to false) until P2609R3: // this was because 'std::iter_value_t<Proj> &' is the same as 'IntWrapper&' // which is not convertible to 'IntWrapper' (implicitly deleted copy ctor) static_assert(std::indirectly_unary_invocable<decltype(print), Proj>); // if the compile-time check above evaluates to true, then this is well-formed: std::ranges::for_each(ints, print, wrap); }
输出
1 2 3 4 5 6 7 8 9
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
P2609R3 | C++20 | 一些需求根据 std::iter_value_t<I>& 定义 这错误地处理了投影,导致与可调用对象 F& 不兼容 |
根据 /*indirect-value-t*/<I> 定义 以正确处理此类投影 |
P2997R1 | C++20 | 相应的概念要求 F& 满足 invocable 和regular_invocable ,分别使用 std::iter_common_reference_t<I> |
不要求 |