Namespaces
Variants
Views
Actions

std::make_optional

From cppreference.com
< cpp‎ | utility‎ | optional
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
 
Defined in header <optional>
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 from value. Effectively calls std::optional<std::decay_t<T>>(std::forward<T>(value))
2) Creates an optional object constructed in-place from args.... Equivalent to return std::optional<T>(std::in_place, std::forward<Args>(args)...);.
3) Creates an optional object constructed in-place from il and args.... Equivalent to return std::optional<T>(std::in_place, il, std::forward<Args>(args)...);.

Contents

[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] See also

constructs the optional object
(public member function) [edit]