From cppreference.com
Defined in header
template<std::copy_constructibleT>requiresstd::is_object_v<T>classsingle_view:publicranges::view_interface<single_view<T>> (1)(since C++20)
(until C++23)template<std::move_constructibleT>requiresstd::is_object_v<T>classsingle_view:publicranges::view_interface<single_view<T>>(since C++23)namespaceviews{inlineconstexpr/* unspecified */single=/* unspecified */;} (2) (since C++20)Call signature
template<classT>requires/* see below */constexpr/* see below */single(T&&t);(since C++20)1) Produces a
that contains exactly one element of a specified value.
2) The expression views::single(e) is
to single_view<std::decay_t<decltype((e))>>(e) for any suitable subexpression e.
The lifetime of the element is bound to the parent single_view. Copying single_view makes a copy of the element.
Customization point objects
The name views::single denotes a customization point object, which is a const
of a
class type. See
for details.
Data members
Member Description value_the single element of the view
(exposition-only member object*)Member functions
constructs a single_view
(public member function)
returns a pointer to the element
(public member function)
returns a pointer past the element
(public member function)
[static]
returns false
(public static member function)
[static]
returns 1
(public static member function)
returns a pointer to the element
(public member function) Inherited from
(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>)
std::ranges::single_view::single_view
single_view()requiresstd::default_initializable<T>=default; (1) (since C++20)constexprexplicitsingle_view(constT&t); (2)(since C++20)
(until C++23)constexprexplicitsingle_view(constT&t)requiresstd::copy_constructible<T>;(since C++23)constexprexplicitsingle_view(T&&t); (3) (since C++20)template<class...Args>requiresstd::constructible_from<T,Args...>constexprexplicitsingle_view(std::in_place_t,Args&&...args); (4) (since C++20)Constructs a single_view.
1) Default initializes value_, which value-initializes its contained value.
2) Initializes value_ with t.
3) Initializes value_ with std::move(t).
4) Initializes value_ as if by value_{std::in_place,std::forward<Args>(args)...}.
std::ranges::single_view::begin
constexprT*begin()noexcept;constexprconstT*begin()constnoexcept;(since C++20)Equivalent to returndata();.
std::ranges::single_view::end
constexprT*end()noexcept;constexprconstT*end()constnoexcept;(since C++20)Equivalent to returndata()+1;.
std::ranges::single_view::empty
staticconstexprboolempty()noexcept;(since C++20)Equivalent to returnfalse;.
std::ranges::single_view::size
staticconstexprstd::size_tsize()noexcept;(since C++20)Equivalent to return1;.
Makes single_view model /*tiny-range*/ as required by
.
std::ranges::single_view::data
constexprT*data()noexcept;constexprconstT*data()constnoexcept;(since C++20)Returns a pointer to the contained value of value_. The behavior is undefined if value_ does not contains a value.
Deduction guides
template<classT>single_view(T)->single_view<T>;(since C++20)Notes
For a
, the inherited empty member function always returns false, and the inherited operatorbool conversion function always returns true.
Example
Run this code
#include<iomanip>#include<iostream>#include<ranges>#include<string>#include<tuple>intmain(){constexprstd::ranges::single_viewsv1{3.1415};// uses (const T&) constructorstatic_assert(sv1);static_assert(notsv1.empty());std::cout<<"1) *sv1.data(): "<<*sv1.data()<<'\n'<<"2) *sv1.begin(): "<<*sv1.begin()<<'\n'<<"3) sv1.size(): "<<sv1.size()<<'\n'<<"4) distance: "<<std::distance(sv1.begin(),sv1.end())<<'\n';std::stringstr{"C++20"};std::cout<<"5) str = "<<std::quoted(str)<<'\n';std::ranges::single_viewsv2{std::move(str)};// uses (T&&) constructorstd::cout<<"6) *sv2.data(): "<<std::quoted(*sv2.data())<<'\n'<<"7) str = "<<std::quoted(str)<<'\n';std::ranges::single_view<std::tuple<int,double,std::string>>sv3{std::in_place,42,3.14,"😄"};// uses (std::in_place_t, Args&&... args)std::cout<<"8) sv3 holds a tuple: { "<<std::get<0>(sv3[0])<<", "<<std::get<1>(sv3[0])<<", "<<std::get<2>(sv3[0])<<" }\n";}Output:
1) *sv1.data(): 3.1415 2) *sv1.begin(): 3.1415 3) sv1.size(): 1 4) distance: 1 5) str = "C++20" 6) *sv2.data(): "C++20" 7) str = "" 8) sv3 holds a tuple: { 42, 3.14, 😄 } 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 single_view was convertible from
the constructor is made explicit
C++20 single_view did not provide the member function empty()provides empty()
C++20 deduction guides for single_view failed to decay the argument;
views::single copied but not wrapped a single_viewa decaying guide provided;
made always wrapping See also