title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
single_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> single_view class, which is a view that has only one element. |
10/05/2022 |
|
|
|
A view that has only one element. This view is useful for test purposes for calling code that needs to be provided with a view with at least one element in it.
template<std::copy_constructible T>
requires std::is_object_v<T>
class single_view : public ranges::view_interface<single_view<T>>
T
The type of the element.
For a description of the following entries, see View class characteristics
Characteristic | Description |
---|---|
Range adaptor | views::single |
Underlying range | None |
Element type | Specified when the single_view is created |
View iterator category | contiguous_range |
Sized | Always returns 1 |
Is const -iterable |
Yes |
Common range | Yes |
Borrowed range | No |
Member functions | Description |
---|---|
ConstructorsC++20 | Construct a single_view . |
begin C++20 |
Get an iterator to the element. |
data C++20 |
Get a pointer to the element. |
end C++20 |
Get the sentinel at the end of the view. |
size C++20 |
Get the number of elements. Always returns 1 . |
Inherited from view_interface |
Description |
back C++20 |
Get the element. |
empty C++20 |
Test whether the view is empty (always returns false ). |
front C++20 |
Get the element. |
operator[] C++20 |
Get the element at the specified position (only position 0 is valid). |
operator bool C++20 |
Test whether the view isn't empty (always returns false ). |
The best way to create a single_view
is by using the views::single
range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type.
The value in the single_view
can be modified unless the template value is const
. For example: single_view<const float> sv{3.14} // this value can't be modified because it's const
.
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
Create an instance of a single_view
.
1) single_view() = default;
2) constexpr explicit single_view(const T& t);
3) constexpr explicit single_view(T&& t);
4) template<class... Args>
requires constructible_from<T, Args...>
constexpr single_view(in_place_t, Args&&... args);
t
The element value.
For information about the template parameter type, see Template parameters.
The best way to create a single_view
is by using the views::single
range adaptor.
1) Create a single_view
with a single element of the specified type that is default constructed. For example, single_view<float> sv{}
creates a single_view
with a single element of type float
that is default constructed to 0.0
.
2) Create a single_view
with a single element of the specified type that is copy-initialized from the specified argument. For example, single_view<myObjectType> sv{myObject}
creates a single_view
with a single element of type myObjectType
that is copy-initialized from the argument.
3) Create a single_view
with a single element of the specified type that is move-initialized from the argument.
4) Create a single_view
with a single element of the specified type initialized with (std::forward<Args>(args)...)
.
/// requires /std:c++20 or higher
#include <ranges>
#include <iostream>
#include <string>
#include <tuple>
int main()
{
std::ranges::single_view<int> sv{7};
std::cout << sv.front() << " " << *sv.data() << "\n"; // 7 7
std::ranges::single_view<std::tuple<int, std::string>> sv2{{6502, "8-bit"}};
std::cout << std::get<0>(sv2[0]) << " " << std::get<1>(sv2[0]) << "\n"; // 6502 8-bit
}
7 7
6502 8-bit
Get a pointer to the single element in the view.
constexpr T* begin() noexcept;
constexpr const T* begin() const noexcept;
None.
A pointer to the single element inside the single_view
.
Get a pointer to the single element in the single_view
.
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;
None.
A pointer to the element in the single_view
.
Gets a pointer to the sentinel after the element.
constexpr T* end() noexcept;
constexpr const T* end() const noexcept;
None.
A pointer to the sentinel that follows the element.
Get the number of elements in the view. Always returns 1
.
static constexpr size_t size() noexcept;
None.
1