名稱空間
變體
操作

std::projected

來自 cppreference.com
 
 
迭代器庫
迭代器概念
迭代器原語
演算法概念與工具
間接可呼叫概念
常用演算法要求
(C++20)
(C++20)
(C++20)
工具
projected
(C++20)
迭代器介面卡
範圍訪問
(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; // not defined

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

          std::indirectly_regular_unary_invocable<I> Proj >

using projected = /*projected-impl*/<I, Proj>::/*__type*/; // see (3)
(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>; // conditionally present

        std::indirect_result_t<Proj&, I> operator*() const; // not defined
    };

};
(3) (C++26 起)
(僅作說明*)
1) 類模板(C++26 前)別名模板(C++26 起) `projected` 將一個 `indirectly_readable` 型別 `I` 和一個可呼叫物件型別 `Proj` 組合成一個新的 `indirectly_readable` 型別,其引用型別是把 `Proj` 應用到 std::iter_reference_t<I> 的結果。
2)std::incrementable_traits 的特化使得 std::projected<I, Proj>I 也是 weakly_incrementable 型別時,成為 weakly_incrementable 型別。
3) 一個用於避免意外的實參依賴查詢的間接層。成員型別 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<>>); // Error before C++26
static_assert(std::sortable<P*>); // Error before C++26
 
int main()
{
    P a[10] = {}; // ten null pointers
    assert(std::count(a, a + 10, nullptr) == 10); // OK
    assert(std::ranges::count(a, a + 10, nullptr) == 10); // Error before C++26
}

[編輯] 參閱

透過投影計算indirectly_readable型別的值型別
(別名模板)[編輯]