std::optional::optional - cppreference.com (original) (raw)

constexpr optional() noexcept; (1) (since C++17)
constexpr optional( std::nullopt_t ) noexcept; (2) (since C++17)
constexpr optional( const optional& other ); (3) (since C++17)
constexpr optional( optional&& other ) noexcept(/* see below */); (4) (since C++17)
template< class U >optional( const optional<U>& other ); (5) (since C++17) (constexpr since C++20) (conditionally explicit)
template< class U >optional( optional<U>&& other ); (6) (since C++17) (constexpr since C++20) (conditionally explicit)
template< class... Args > constexpr explicit optional( std::in_place_t, Args&&... args ); (7) (since C++17)
template< class U, class... Args > constexpr explicit optional( std::in_place_t, std::initializer_list<U> ilist, Args&&... args ); (8) (since C++17)
template< class U = std::remove_cv_t<T> > constexpr optional( U&& value ); (9) (since C++17) (conditionally explicit)

Constructs a new optional object.

Contents

[edit] Parameters

other - another optional object whose contained value is copied
value - value with which to initialize the contained value
args... - arguments with which to initialize the contained value
ilist - initializer list with which to initialize the contained value

[edit] Effects

Overload Initialization method Initializer for the contained value has_value() after construction
(1) N/A - false
(2)
(3) Direct (non-list) *other other.has_value() If false, the contained value is not initialized.
(4) std::move(*other)
(5) *other
(6) std::move(*other)
(7) std::forward<Args>(args)... true
(8) ilist, std::forward<Args>(args)...
(9) std::forward<U>(value)

[edit] Constraints and supplement information

  1. This overload participates in overload resolution only if all following conditions are satisfied:
  1. This overload participates in overload resolution only if all following conditions are satisfied:
  1. This overload participates in overload resolution only if std::is_constructible_v<T, Args...> is true.

If T’s constructor selected for the initialization is a constexpr constructor, this constructor is also a constexpr constructor.

If T’s constructor selected for the initialization is a constexpr constructor, this constructor is also a constexpr constructor.

If T’s constructor selected for the initialization is a constexpr constructor, this constructor is also a constexpr constructor.

  1. 1.0 1.1 In other words, T is neither constructible nor convertible from any expression of type (possibly const-qualified) std::optional<U>

[edit] Exceptions

  1. Throws any exception thrown by the constructor of T.

  2. Throws any exception thrown by the constructor of T. Has the following

5-9) Throws any exception thrown by the constructor of T.

[edit] Deduction guides

[edit] Notes

Before the resolution of LWG issue 3836, constructing an std::optional<bool> from std::optional<U> would select overload (9) instead of overloads (5,6) if U is not bool. This is because overloads (5,6) did not participate in overload resolution if T (bool in this case) can be constructed or converted from std::optional<U>, but std::optional::operator bool makes the conversion possible for any U.

As a result, the constructed std::optional<bool> always contains a value. That value is determined by whether the provided std::optional<U> object contains a value, rather than the bool value direct-initialized from the contained value:

std::optional op_false(false); std::optional op_zero(0);   std::optional from_bool(op_false); // OK: contains 0 (initialized from false) std::optional from_int(op_zero); // DEFECT (LWG 3836): contains true because // op_zero contains a value, even if initializing // bool from that value gives false

Feature-test macro Value Std Feature
__cpp_lib_optional 202106L (C++20)(DR20) Fully constexpr (5,6)

[edit] Example

#include #include #include   int main() { std::optional o1, // empty o2 = 1, // init from rvalue o3 = o2; // copy-constructor   // calls std::string( initializer_list ) constructor std::optional<std::string> o4(std::in_place, {'a', 'b', 'c'});   // calls std::string( size_type count, CharT ch ) constructor std::optional<std::string> o5(std::in_place, 3, 'A');   // Move-constructed from std::string using deduction guide to pick the type   std::optional o6(std::string{"deduction"});   std::cout << *o2 << ' ' << *o3 << ' ' << *o4 << ' ' << *o5 << ' ' << *o6 << '\n'; }

Output:

[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
LWG 3836 C++17 when constructing an std::optional<bool>from std::optional<U>, the overload resolutionwould select overload (9) if U is not bool always selects theconverting copy/moveconstructor in this case
LWG 3886 C++17 the default template argument of overload (9) was T changed to std::remove_cv_t<T>
P0602R4 C++17 copy/move constructors might not be trivialeven if underlying constructor is trivial required topropagate triviality
P2231R1 C++20 overloads (5,6) from another std::optional was not constexpr made constexpr

[edit] See also