std::projected(3)
β¨ std::projected(3) β¨
okay so bestie, this is from the C++ Standard Libary manpage (yes, "Libary" β that's literally how it's spelled in the header, no cap π). it's giving C++20/26 template metaprogramming realness and i'm obsessed. let's go π«Ά
πΈ NAME πΈ
std::projected β std::projected (yes the name is literally the same, it's iconic, periodt π
)
β¨ Synopsis β¨
here's the whole vibe. don't panic, it's just templates:
Defined in header <iterator>
template< std::indirectly_readable I,
std::indirectly_regular_unary_invocable<I> Proj >
struct projected
{
(since C++20)
using value_type =
(until C++26)
std::remove_cvref_t<std::indirect_result_t<Proj&, I>>;
std::indirect_result_t<Proj&, I> operator*() const; //
not defined
};
template< std::indirectly_readable I,
std::indirectly_regular_unary_invocable<I> Proj >
(since C++26)
using projected = /*projected-impl*/<I, Proj>::/*__type*/; //
see (3)
template< std::weakly_incrementable I, class Proj >
struct incrementable_traits<std::projected<I, Proj>> (1)
(since C++20)
{ (2)
(until C++26)
using difference_type = std::iter_difference_t<I>;
};
template< class I, class Proj >
struct /*projected-impl*/
{
struct /*__type*/
{
using value_type =
(since C++26)
std::remove_cvref_t<std::indirect_result_t<Proj&, I>>; (3)
(exposition
using difference_type = std::iter_difference_t<I>; //
only*)
conditionally present
std::indirect_result_t<Proj&, I> operator*() const;
// not defined
};
};
now the numbered tea β:
- (1) okay so it's a Class until C++26, and an Alias since C++26. the template
projectedcombines anindirectly_readabletypeIand a callable object typeProjinto a newindirectly_readabletype whose reference type is the result of applyingProjto thestd::iter_reference_t<I>. slay π - (2) this specialization of
std::incrementable_traitsmakesstd::projected<I, Proj>aweakly_incrementabletype whenIis also aweakly_incrementabletype. periodt. - (3) an indirect layer used for avoiding unexpected argument-dependent lookup. the member type
difference_typeexists only ifImodelsweakly_incrementable. iykyk.
and like, projected is used only to constrain algorithms that accept callable objects and projections, and hence its operator*() is not defined. so don't try to call it, bestie π
π Template parameters π
Iβ an indirectly readable typeProjβ projection applied to a dereferencedI
π¦ Notes π¦
the indirect layer stops I and Proj from being associated classes of projected. when an associated class of I or Proj is an incomplete class type, the indirect layer saves you from the unnecessary attempt to inspect the definition of that type, which would result in a hard error. no cap, that's a whole mood π«Ά
π₯ Example π₯
// Run this code
#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
}
π See also π
projected_value_t(C++26) (alias template) β computes the value type of anindirectly_readabletype by projection.
okay that's the whole thing bestie! source: http://cppreference.com π version/date: 2024.06.10 β¨ page: std::projected(3) π now go slay those templates π π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms