Assignable wrapper (C++20) - cppreference.com (original) (raw)
ranges::single_view, ranges::repeat_view,(since C++23) and range adaptors that store an invocable object are specified in terms of an exposition-only class template _copyable-box_
(until C++23)_movable-box_
(since C++23). The name shown here is for exposition purposes only.
The wrapper behaves exactly like std::optional<T>, except that the default constructor, copy assignment operator, and move assignment operator are (conditionally) different from those of std::optional, which augments T
with assignability when needed and makes it always satisfy copyableor movable(since C++23).
Contents
- 1 Template parameters
- 2 Member functions
- 3 Default constructor
- 4 Assignment operators
- 5 Members identical to std::optional
[edit] Template parameters
[edit] Member functions
Default constructor
The default constructor is provided if and only if T
models default_initializable.
A default-constructed wrapper contains a value-initialized T
object.
Assignment operators
| (1) | | | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | --------------------------- | | constexpr /*copyable-box*/& operator=(const /*copyable-box*/& other); noexcept(/* see below */); | | (since C++20) (until C++23) | | constexpr /*movable-box*/& operator=(const /*movable-box*/& other); noexcept(/* see below */) requires std::copy_constructible<T>; | | (since C++23) | | (2) | | | | constexpr /*copyable-box*/& operator=(/*copyable-box*/&& other) noexcept(std::is_nothrow_move_constructible_v<T>); | | (since C++20) (until C++23) | | constexpr /*movable-box*/& operator=(/*movable-box*/&& other) noexcept(std::is_nothrow_move_constructible_v<T>); | | (since C++23) |
Members identical to std::optional
Member functions
(constructor) | constructs the optional object (public member function of std::optional) [edit] |
---|---|
(destructor) | destroys the contained value, if there is one (public member function of std::optional) [edit] |
operator= | assigns contents (public member function of std::optional) [edit] |
Observers | |
operator->operator* | accesses the contained value (public member function of std::optional) [edit] |
operator boolhas_value | checks whether the object contains a value (public member function of std::optional) [edit] |
Modifiers | |
reset | destroys any contained value (public member function of std::optional) [edit] |
emplace | constructs the contained value in-place (public member function of std::optional) [edit] |
[edit] Notes
A _copyable-box_
(until C++23)_movable-box_
(since C++23) does not contain a value only if
T
does not model movable or copyable, and an exception is thrown on move assignment or copy assignment respectively, or- it is initialized/assigned from another valueless wrapper.
Before P2325R3, the wrapper was called _semiregular-box_
in the standard and always satisfied semiregular, as the default constructor was always provided (which might construct a valueless wrapper).
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges | 201911L | (C++20) | Ranges library and constrained algorithms |
202106L | (C++20)(DR) | Non-default-initializable views | |
202207L | (C++23) | Relaxing range adaptors to allow for move-only types |
[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 |
---|---|---|---|
P2325R3 | C++20 | if T is not default_initializable, the default constructorconstructs a wrapper which does not contain a value | the wrapper is alsonot default_initializable |
LWG 3572 | C++20 | conditionally different assignment operators were not constexpr | made constexpr |
[edit] See also
ranges::single_viewviews::single(C++20) | a view that contains a single element of a specified value(class template) (customization point object)[edit] |
---|---|
ranges::repeat_viewviews::repeat(C++23) | a view consisting of a generated sequence by repeatedly producing the same value(class template) (customization point object)[edit] |
ranges::filter_viewviews::filter(C++20) | a view that consists of the elements of a range that satisfies a predicate(class template) (range adaptor object)[edit] |
ranges::transform_viewviews::transform(C++20) | a view of a sequence that applies a transformation function to each element(class template) (range adaptor object)[edit] |
ranges::take_while_viewviews::take_while(C++20) | a view consisting of the initial elements of another view, until the first element on which a predicate returns false(class template) (range adaptor object)[edit] |
ranges::drop_while_viewviews::drop_while(C++20) | a view consisting of the elements of another view, skipping the initial subsequence of elements until the first element where the predicate returns false(class template) (range adaptor object)[edit] |
ranges::zip_transform_viewviews::zip_transform(C++23) | a view consisting of results of application of a transformation function to corresponding elements of the adapted views(class template) (customization point object)[edit] |
ranges::adjacent_transform_viewviews::adjacent_transform(C++23) | a view consisting of results of application of a transformation function to adjacent elements of the adapted view(class template) (range adaptor object)[edit] |