Files
cppdraft_translate/cppdraft/range/single.md
2025-10-25 03:02:53 +03:00

5.2 KiB
Raw Blame History

[range.single]

25 Ranges library [ranges]

25.6 Range factories [range.factories]

25.6.3 Single view [range.single]

25.6.3.1 Overview [range.single.overview]

1

#

single_view produces a view that contains exactly one element of a specified value.

2

#

The name views::single denotes a customization point object ([customization.point.object]).

Given a subexpression E, the expressionviews::single(E) is expression-equivalent tosingle_view<decay_t<decltype((E))>>(E).

3

#

[Example 1: for (int i : views::single(4)) cout << i; // prints 4 — end example]

25.6.3.2 Class template single_view [range.single.view]

🔗

namespace std::ranges {template<move_constructible T>requires is_object_vclass single_view : public view_interface<single_view> {private:movable-box value_; // exposition only (see [range.move.wrap])public: single_view() requires default_initializable = default; constexpr explicit single_view(const T& t) requires copy_constructible; constexpr explicit single_view(T&& t); template<class... Args>requires constructible_from<T, Args...>constexpr explicit single_view(in_place_t, Args&&... args); constexpr T* begin() noexcept; constexpr const T* begin() const noexcept; constexpr T* end() noexcept; constexpr const T* end() const noexcept; static constexpr bool empty() noexcept; static constexpr size_t size() noexcept; constexpr T* data() noexcept; constexpr const T* data() const noexcept; }; template single_view(T) -> single_view;}

🔗

constexpr explicit single_view(const T& t) requires [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14Concept copy_­constructible[concept.copyconstructible]")<T>;

1

#

Effects: Initializes value_ with t.

🔗

constexpr explicit single_view(T&& t);

2

#

Effects: Initializes value_ with std::move(t).

🔗

template<class... Args> requires [constructible_from](concept.constructible#concept:constructible_from "18.4.11Concept constructible_­from[concept.constructible]")<T, Args...> constexpr explicit single_view(in_place_t, Args&&... args);

3

#

Effects: Initializes value_ as if byvalue_{in_place, std::forward(args)...}.

🔗

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

4

#

Effects: Equivalent to: return data();

🔗

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

5

#

Effects: Equivalent to: return data() + 1;

🔗

static constexpr bool empty() noexcept;

6

#

Effects: Equivalent to: return false;

🔗

static constexpr size_t size() noexcept;

7

#

Effects: Equivalent to: return 1;

🔗

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

8

#

Effects: Equivalent to: return value_.operator->();