constexpr optional() noexcept; (since
constexpr optional( std::nullopt_t ) (1) C++17)
noexcept;
constexpr optional( const optional& other (2) (since
); C++17)
constexpr optional( optional&& other ) (3) (since
noexcept(/* see below */); C++17)
(since C++17)
template < class U > (until C++20)
optional( const optional<U>& other ); (conditionally
explicit)
template < class U > (since C++20)
constexpr optional( const optional<U>& (conditionally
other ); explicit)
(since C++17)
template < class U > (until C++20)
optional( optional<U>&& other ); (conditionally
explicit)
template < class U > (since C++20)
constexpr optional( optional<U>&& other ); (conditionally
(4) explicit)
template< class... Args >
constexpr explicit optional( (6) (since C++17)
std::in_place_t, Args&&... args ); (5)
template< class U, class... Args >
constexpr explicit optional(
std::in_place_t, (7) (since C++17)
std::initializer_list<U> ilist,
Args&&... args );
template < class U = T > (since C++17)
constexpr optional( U&& value ); (8) (conditionally
explicit)
Constructs a new optional object.
1) Constructs an object that does not contain a value.
2) Copy constructor: If other contains a value, initializes the contained
value as
if direct-initializing (but not direct-list-initializing) an object of type T
with
the expression *other. If other does not contain a value, constructs an
object that
does not contain a value.
* This constructor is defined as deleted if
std::is_copy_constructible_v<T> is
false.
* It is a trivial constructor if
std::is_trivially_copy_constructible_v<T> is
true.
3) Move constructor: If other contains a value, initializes the contained
value as
if direct-initializing (but not direct-list-initializing) an object of type T
with
the expression std::move(*other) and does not make other empty: a moved-from
optional still contains a value, but the value itself is moved from. If other
does
not contain a value, constructs an object that does not contain a value.
* This constructor does not participate in overload resolution unless
std::is_move_constructible_v<T> is true.
* It is a trivial constructor if
std::is_trivially_move_constructible_v<T> is
true.
4) Converting copy constructor: If other doesn't contain a value, constructs
an
optional object that does not contain a value. Otherwise, constructs an
optional
object that contains a value, initialized as if direct-initializing (but not
direct-list-initializing) an object of type T with the expression *other.
* This constructor does not participate in overload resolution unless the
following conditions are met:
* std::is_constructible_v<T, const U&> is true.
* T is not constructible or convertible from any expression of type (possibly
const) std::optional<U>, i.e., the following 8 type traits are all
false:
* std::is_constructible_v<T, std::optional<U>&>
* std::is_constructible_v<T, const std::optional<U>&>
* std::is_constructible_v<T, std::optional<U>&&>
* std::is_constructible_v<T, const std::optional<U>&&>
* std::is_convertible_v<std::optional<U>&, T>
* std::is_convertible_v<const std::optional<U>&, T>
* std::is_convertible_v<std::optional<U>&&, T>
* std::is_convertible_v<const std::optional<U>&&, T>
* This constructor is explicit if and only if std::is_convertible_v<const
U&, T>
is false.
5) Converting move constructor: If other doesn't contain a value, constructs
an
optional object that does not contain a value. Otherwise, constructs an
optional
object that contains a value, initialized as if direct-initializing (but not
direct-list-initializing) an object of type T with the expression
std::move(*other).
* This constructor does not participate in overload resolution unless the
following conditions are met:
* std::is_constructible_v<T, U&&> is true.
* T is not constructible or convertible from any expression of type (possibly
const) std::optional<U>, i.e., the following 8 type traits are all
false:
* std::is_constructible_v<T, std::optional<U>&>
* std::is_constructible_v<T, const std::optional<U>&>
* std::is_constructible_v<T, std::optional<U>&&>
* std::is_constructible_v<T, const std::optional<U>&&>
* std::is_convertible_v<std::optional<U>&, T>
* std::is_convertible_v<const std::optional<U>&, T>
* std::is_convertible_v<std::optional<U>&&, T>
* std::is_convertible_v<const std::optional<U>&&, T>
* This constructor is explicit if and only if
std::is_convertible_v<U&&, T> is
false.
6) Constructs an optional object that contains a value, initialized as if
direct-initializing (but not direct-list-initializing) an object of type T
from the
arguments std::forward<Args>(args)....
* If the selected constructor of T is a constexpr constructor, this
constructor is
a constexpr constructor.
* The function does not participate in the overload resolution unless
std::is_constructible_v<T, Args...> is true
7) Constructs an optional object that contains a value, initialized as if
direct-initializing (but not direct-list-initializing) an object of type T
from the
arguments ilist, std::forward<Args>(args)....
* If the selected constructor of T is a constexpr constructor, this
constructor is
a constexpr constructor.
* The function does not participate in the overload resolution unless
std::is_constructible_v<T, std::initializer_list<U>&,
Args&&...> is true
8) Constructs an optional object that contains a value, initialized as if
direct-initializing (but not direct-list-initializing) an object of type T
with the
expression std::forward<U>(value).
* If the selected constructor of T is a constexpr constructor, this
constructor is
a constexpr constructor.
* This constructor does not participate in overload resolution unless
std::is_constructible_v<T, U&&> is true and
std::decay_t<U>
(until C++20)
std::remove_cvref_t<U>
(since C++20) is neither std::in_place_t nor std::optional<T>.
* This constructor is explicit if and only if
std::is_convertible_v<U&&, T> is
false.