std::projected
在标头 <iterator> 定义 | ||
(1) | ||
template<std::indirectly_readable I, std::indirectly_regular_unary_invocable<I> Proj > | (C++20 起) (C++26 前) | |
template<std::indirectly_readable I, std::indirectly_regular_unary_invocable<I> Proj > | (C++26 起) | |
template<std::weakly_incrementable I, class Proj > struct incrementable_traits<std::projected<I, Proj>> | (2) | (C++20 起) (C++26 前) |
辅助模板 | ||
template<class I, class Proj > struct/*projected-impl*/ | (3) | (C++26 起) (仅用于阐述*) |
projected
将 indirectly_readable
类型 I
与可调用对象类型 Proj
组合到新的 indirectly_readable
类型中,它的引用类型是应用 Proj
到 std::iter_reference_t<I> 的结果。I
是 weakly_incrementable
类型时也是 weakly_incrementable
类型。difference_type
只有在 I
实现 weakly_incrementable
时才存在。projected
仅用于约束接受可调用对象与投影的算法,因此不会定义它的 operator*()。
目录 |
[编辑]模板形参
I | - | 间接可读类型 |
Proj | - | 应用到解引用的 I 上的投影 |
[编辑]注解
间接层使得 I
与 Proj
不会成为 projected
的关联类。当一个 I
或 Proj
的关联类为不完整类类型时,间接层避免检查该类型的定义的尝试,而尝试会导致硬错误。
[编辑]示例
#include <algorithm>#include <cassert>#include <functional>#include <iterator> template<class T>struct Holder { T t;}; struct Incomplete; using P = Holder<Incomplete>*; static_assert(std::equality_comparable<P>);// OK static_assert(std::indirectly_comparable<P*, P*, std::equal_to<>>);// C++26 前是错误 static_assert(std::sortable<P*>);// C++26 前是错误 int main(){ P a[10]={};// 十个空指针assert(std::count(a, a +10, nullptr)==10);// OKassert(std::ranges::count(a, a +10, nullptr)==10);// C++26 前是错误}
[编辑]参阅
(C++26) | 计算 indirectly_readable 类型投影后的值类型 (别名模板) |