Namespaces
Variants
Actions

std::optional<T>::optional

From cppreference.com
< cpp‎ | utility‎ | optional
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
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

3) If std::is_copy_constructible_v<T> is false, the constructor is defined as deleted.
If std::is_trivially_copy_constructible_v<T> is true, the constructor is trivial.
4) This overload participates in overload resolution only if std::is_move_constructible_v<T> is true.
If std::is_trivially_move_constructible_v<T> is true, the constructor is trivial.
5) This overload participates in overload resolution only if all following conditions are satisfied: