Produces a
view
that contains exactly one element of a specified value.
The lifetime of the element is bound to the parent single_view
. Copying single_view
makes a copy of the element.
The name views::single
denotes a customization point object, which is a const function object of a literal semiregular
class type. See CustomizationPointObject for details.
copyable-box
<T>
value_
(until C++23) the single element of the view
movable-box
<T>
value_
(since C++23) the single element of the view
single_view
std::ranges::view_interface<D>
) [edit] returns a sentinel for the constant iterator of the range
std::ranges::view_interface<D>
) [edit] returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
std::ranges::view_interface<D>
) [edit] returns the first element in the derived view, provided if it satisfies forward_range
std::ranges::view_interface<D>
) [edit] returns the last element in the derived view, provided only if it satisfies bidirectional_range
and common_range
std::ranges::view_interface<D>
) [edit] returns the n
th element in the derived view, provided only if it satisfies random_access_range
std::ranges::view_interface<D>
) [edit] std::ranges::single_view::single_view (1) (since C++20) (2)
constexpr explicit single_view( const T& t );
(since C++20)constexpr explicit single_view( T&& t );
(3) (since C++20) (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).
constexpr T* begin() noexcept;
constexpr const T* begin() const noexcept;
Equivalent to return data();.
std::ranges::single_view::endconstexpr T* end() noexcept;
constexpr const T* end() const noexcept;
Equivalent to return data() + 1;.
std::ranges::single_view::emptystatic constexpr bool empty() noexcept;
(since C++20)Equivalent to return false;.
std::ranges::single_view::size (since C++20)Equivalent to return 1;.
Makes single_view
model /*tiny-range*/ as required by split_view
.
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;
Returns a pointer to the contained value of value_
. The behavior is undefined if value_
does not contains a value.
template< class T >
single_view( T ) -> single_view<T>;
For a single_view
, the inherited empty
member function always returns false, and the inherited operator bool conversion function always returns true.
#include <iomanip> #include <iostream> #include <ranges> #include <string> #include <tuple> int main() { constexpr std::ranges::single_view sv1{3.1415}; // uses (const T&) constructor static_assert(sv1); static_assert(not sv1.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::string str{"C++20"}; std::cout << "5) str = " << std::quoted(str) << '\n'; std::ranges::single_view sv2{std::move(str)}; // uses (T&&) constructor std::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, ð }[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 3428 C++20single_view
was convertible from std::in_place_t the constructor is made explicit LWG 4035 C++20 single_view
did not provide the member function empty()
provides empty()
P2367R0 C++20 deduction guides for single_view
failed to decay the argument;
views::single
copied but not wrapped a single_view
a decaying guide provided;
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4