std::projected
Определено в заголовке <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 below
| (с 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*/ { // exposition only
struct /*__type*/ { // exposition only
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
}
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/iterator/projected