Semiregular wrapper (C++20)

From cppreference.com
< cpp‎ | ranges
 
 
 
template<class T>

    requires std::copy_constructible<T> && std::is_object_v<T>

class /*semiregular-box*/;
(since C++20)

ranges::single_view, ranges::filter_view and ranges::transform_view are specified in terms of an exposition-only class template semiregular_box. The name semiregular_box is for exposition purposes only and not normative.

semiregular_box<T> behaves exactly like std::optional<T> with a little differences, which makes it model semiregular.

Template parameters

T - the type of the value to manage initialization state for. The type must be a object type and model copy_constructible

Member functions

Following member functions are conditionally different from corresponding member functions of std::optional.

semiregular_box::semiregular_box

constexpr semiregular_box() noexcept(std::is_nothrow_default_constructible_v<T>);

If T models default_initializable, the default constructor of semiregular_box<T> constructs a semiregular wrapper containing a value-initialized T and is equivalent to:

constexpr semiregular_box() noexcept(std::is_nothrow_default_constructible_v<T>)
  : semiregular_box{std::in_place}
{ }

Otherwise, the default constructor is equivalent to the default constructor of std::optional and constructs a semiregular wrapper which does not contain a value.

semiregular_box::operator=

semiregular_box& operator=(const semiregular_box& other)
    noexcept(std::is_nothrow_copy_constructible_v<T>)
(1)
semiregular_box& operator=(semiregular_box&& other)
    noexcept(std::is_nothrow_move_constructible_v<T>)
(2)
1) If Assignable<T&, const T&> is not satisfied, the copy assignment operator's body is equivalent to if (other) emplace(*other); else reset(); return *this;.
Otherwise, the copy assignment operator is identical to the copy assignment operator of std::optional.
2) If Assignable<T&, T> is not satisfied, the move assignment operator's body is equivalent to if (other) emplace(std::move(*other)); else reset(); return *this;.
Otherwise, the move assignment operator is identical to the move assignment operator of std::optional.

Members identical to std::optional

Member functions

constructs the optional object
(public member function of std::optional<T>)
destroys the contained value, if there is one
(public member function of std::optional<T>)
assigns contents
(public member function of std::optional<T>)
Observers
accesses the contained value
(public member function of std::optional<T>)
checks whether the object contains a value
(public member function of std::optional<T>)
Modifiers
destroys any contained value
(public member function of std::optional<T>)
constructs the contained value in-place
(public member function of std::optional<T>)