Namespaces
Variants
Views
Actions

std::ranges::views::single, std::ranges::single_view

From cppreference.com
< cpp‎ | ranges
 
 
 
Defined in header <ranges>
template< std::copy_constructible T >

  requires std::is_object_v<T>

class single_view : public ranges::view_interface<single_view<T>>
(1) (since C++20)
namespace views {

    inline constexpr /*unspecified*/ single = /*unspecified*/;

}
(2) (since C++20)
1) Produces a view that contains exactly one element of a specified value.
2) The expression views::single(E) is expression-equivalent to single_view{E} for any suitable subexpression E.

Typical implementations of single_view store a single member of type semiregular_box<T>. For description purposes, this member is hereinafter called value_.

The lifetime of the element is bound to the parent single_view. Copying single_view makes a copy of the element.

Contents

[edit] Expression-equivalent

Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.

[edit] Member functions

(constructor)
constructs a single_view
(public member function)
begin
returns a pointer to the element
(public member function)
end
returns a pointer past the element
(public member function)
size
[static]
returns 1 (one)
(public static member function)
data
returns a pointer to the element
(public member function)

std::ranges::single_view::single_view

single_view() = default;
(1)
constexpr explicit single_view( const T& t );
(2)
constexpr explicit single_view( T&& t );
(3)
template< class... Args >

  requires std::constructible_from<T, Args...>

constexpr explicit single_view( std::in_place_t, Args&&... args );
(4)

Constructs a single_view.

1) Default initializes 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

constexpr T* begin() noexcept;
constexpr const T* begin() const noexcept;

Equivalent to return data();.

std::ranges::single_view::end

constexpr T* end() noexcept;
constexpr const T* end() const noexcept;

Equivalent to return data() + 1;.

std::ranges::single_view::size

static constexpr std::size_t size() noexcept;

Equivalent to return 1;.

This is a static function. This makes single_view model /*tiny-range*/ as required by split_view.

std::ranges::single_view::data

constexpr T* data() noexcept;
constexpr const T* data() const noexcept;

Returns value_.operator->().

[edit] Example

#include <iostream>
#include <ranges>
#include <string>
#include <tuple>
 
int main()
{
    std::ranges::single_view sv1{3.1415}; // uses (const T&) constructor
 
    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 = \"" << str << "\"\n";
    std::ranges::single_view sv2{std::move(str)}; // uses (T&&) constructor
    std::cout << "6) *sv2.data(): \"" << *sv2.data() << "\"\n"
              << "7)  str = \"" << 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 keeps 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 keeps a tuple: { 42, 3.14, 😄 }

[edit] See also

an empty view with no elements
(class template) (variable template) [edit]
a view over the subranges obtained from splitting another view using a delimiter
(class template) (range adaptor object) [edit]