From cppreference.com
Defined in header
template<ranges::input_rangeV,std::size_tN>requiresranges::view<V>&&/*has-tuple-element*/<ranges::range_value_t<V>,N>&&/*has-tuple-element*/<std::remove_reference_t<ranges::range_reference_t<V>>,N>&&/*returnable-element*/<ranges::range_reference_t<V>,N>classelements_view:publicranges::view_interface<elements_view<V,N>>; (1) (since C++20)namespaceviews{template<std::size_tN>constexpr/* unspecified */elements=/* unspecified */;} (2) (since C++20)Call signature
template<ranges::viewable_rangeR>requires/* see below */constexprranges::viewautoelements<N>(R&&r);(since C++20)Helper concepts
template<classT,std::size_tN>concept/*has-tuple-element*/=requires(Tt){typenamestd::tuple_size<T>::type;requiresN<std::tuple_size_v<T>;typenamestd::tuple_element_t<N,T>;{std::get<N>(t)}->std::convertible_to<conststd::tuple_element_t<N,T>&>;}; (3)(until C++23)
(exposition only*)template<classT,std::size_tN>concept/*has-tuple-element*/=/*tuple-like*/<T>&&N<std::tuple_size_v<T>&&requires(Tt){{std::get<N>(t)}->std::convertible_to<conststd::tuple_element_t<N,T>&>;};(since C++23)
(exposition only*)template<classT,std::size_tN>conceptreturnable-element=std::is_reference_v<T>||std::move_constructible<std::tuple_element_t<N,T>>; (4) (exposition only*)1) Accepts a
of tuple-like values, and issues a view with a value type of the Nth element of the adapted view's value-type.
2) Every specialization of views::elements is a
. The expression views::elements<M>(e) is
to elements_view<views::all_t<decltype((e))>,M>{e} for any suitable subexpression e and constant expression M.
3) Ensures that the elements of the underlying view are tuple-like values, see
(since C++23).
4) Ensures that dangling references cannot be returned.
elements_view models the concepts
,
,
,
,
, and
when the underlying view V models respective concepts.
Data members
Member Description Vbase_the underlying view
(exposition-only member object*)Member functions
constructs a elements_view
(public member function)
returns a copy of the underlying (adapted) view
(public member function)
returns an iterator to the beginning
(public member function)
returns an iterator or a sentinel to the end
(public member function)
returns the number of elements, provided only if the underlying (adapted) range satisfies
(public member function)
(C++26)
returns the approximate size of the resulting
(public member function)
Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
returns the nth element in the derived view, provided only if it satisfies
(public member function of std::ranges::view_interface<D>)
Nested classes
the iterator type
(exposition-only member class template*)the sentinel type
(exposition-only member class template*)Helper templates
template<classT,std::size_tN>constexprboolenable_borrowed_range<std::ranges::elements_view<T,N>>=ranges::enable_borrowed_range<T>;(since C++20)This specialization of
makes elements_view satisfy
when the underlying view satisfies it.
Example
Run this code
importstd;intmain(){conststd::vector<std::tuple<int,char,std::string>>vt{{1,'A',"α"},{2,'B',"β"},{3,'C',"γ"},{4,'D',"δ"},{5,'E',"ε"},};std::println("{:n:}",vt|std::views::elements<0>);std::println("{:n:}",vt|std::views::elements<1>);std::println("{:n:}",vt|std::views::elements<2>);}Output:
1, 2, 3, 4, 5 A, B, C, D, E α, β, γ, δ, ε Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++20 elements_view was never a borrowed_rangeit is a borrowed_range
if its underlying view is
C++20 dangling reference could be obtained from elements_viewsuch usage is forbidden See also