Spec-Zone.ru › C++

std::projected

Определён в заголовочном файле <iterator>
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
};
(1) (начиная с 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> типом weakly_incrementable, если I также является типом 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
}

См. также

projected_value_t
(C++26)
вычисляет тип значения типа indirectly_readable посредством проекции
(шаблон псевдонима)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/iterator/projected

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API