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

Defined in header
template< class T > constexpr std::optional<std::decay_t<T>> make_optional( T&& value ); (1) (since C++17)
template< class T, class... Args > constexpr std::optional<T> make_optional( Args&&... args ); (2) (since C++17)
template< class T, class U, class... Args > constexpr std::optional<T> make_optional( std::initializer_list<U> il, Args&&... args ); (3) (since C++17)
  1. Creates an optional object constructed in-place from args.... Equivalent to return std::optional<T>(std::in_place, std::forward<Args>(args)...);.
    This overload participates in overload resolution only if std::is_constructible_v<T, Args...> is true.

[edit] Parameters

value - the value to construct optional object with
il, args - arguments to be passed to the constructor of T

[edit] Return value

The constructed optional object.

[edit] Exceptions

Throws any exception thrown by the constructor of T.

[edit] Notes

T need not be movable for overloads (2,3) due to guaranteed copy elision.

[edit] Example

Possible output:

op1: a,b,c, op2: 2,2,2,2,2, op3: "hello world" str: ""

[edit] See also