std::indirectly_unary_invocable, std::indirectly_regular_unary_invocable
来自cppreference.com
| 在标头 <iterator> 定义
|
||
std::indirectly_unary_invocable |
||
| |
(C++20 起) | |
std::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)>;
// P2609R3 前为错误(求值为 false):
// 这是因为 'std::iter_value_t<Proj> &' 与 'IntWrapper&' 相同
// 而它不能转换为 'IntWrapper'(隐式弃置了复制构造函数)
static_assert(std::indirectly_unary_invocable<decltype(print), Proj>);
// 如果上面的编译时检查求值为 true,那么以下是良构的:
std::ranges::for_each(ints, print, wrap);
}
输出:
1 2 3 4 5 6 7 8 9
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| 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>
|
不作此要求 |