Semiregular wrapper (C++20)

< cpp‎ | ranges
template<class T>

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

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

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

semiregular<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 CopyConstructible

Member functions

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

semiregular::semiregular

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

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

constexpr semiregular() noexcept(std::is_nothrow_default_constructible_v<T>)
  : semiregular{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::operator=

semiregular& operator=(const semiregular& other)
    noexcept(std::is_nothrow_copy_constructible_v<T>)
(1)
semiregular& operator=(semiregular&& 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>)