Namespaces
Variants

std:: projected

From cppreference.net
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
projected
(C++20)
Iterator adaptors
Range access
(C++11) (C++14)
(C++14) (C++14)
(C++11) (C++14)
(C++14) (C++14)
(C++17) (C++20)
(C++17)
(C++17)
定义于头文件 <iterator>
(1)
template < std:: indirectly_readable I,

std:: indirectly_regular_unary_invocable < I > Proj >
struct projected
{
using value_type = std:: remove_cvref_t
< std:: indirect_result_t < Proj & , I >> ;
std:: indirect_result_t < Proj & , I > operator * ( ) const ; // 未定义

} ;
(C++20 起)
(C++26 前)
template < std:: indirectly_readable I,

std:: indirectly_regular_unary_invocable < I > Proj >

using projected = /*projected-impl*/ < I, Proj > :: /*type*/ ;
(C++26 起)
template < std:: weakly_incrementable I, class Proj >

struct incrementable_traits < std :: projected < I, Proj >>
{
using difference_type = std:: iter_difference_t < I > ;

} ;
(2) (C++20 起)
(C++26 前)
辅助模板
template < class I, class Proj >

struct /*projected-impl*/
{
struct /*type*/
{
using value_type = std:: remove_cvref_t
< std:: indirect_result_t < Proj & , I >> ;
using difference_type =
std:: iter_difference_t < I > ; // 有条件存在
std:: indirect_result_t < Proj & , I > operator * ( ) const ; // 未定义
} ;

} ;
(3) (C++26 起)
( 仅用于说明* )
1) (C++26 前) 别名 (C++26 起) 模板 projected 将一个 indirectly_readable 类型 I 与可调用对象类型 Proj 组合成新的 indirectly_readable 类型,其引用类型是 Proj 应用于 std:: iter_reference_t < I > 的结果。
2) I 同样是 weakly_incrementable 类型时,此 std::incrementable_traits 的特化使得 std :: projected < I, Proj > 成为 weakly_incrementable 类型。
3) 用于避免意外 实参依赖查找 的间接层。
对于仅用于说明的嵌套类 /*type*/ ,其嵌套类型 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>); // 正常
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); // 正常
    assert(std::ranges::count(a, a + 10, nullptr) == 10); // C++26 前报错
}

参见

通过投影计算 indirectly_readable 类型的值类型
(别名模板)