constexpr optional(); constexpr optional( std::nullopt_t ); | (1) | (since C++17) |
optional( const optional& other ); | (2) | (since C++17) |
optional( optional&& other ); | (3) | (since C++17) |
constexpr optional( const T& value ); | (4) | (since C++17) |
constexpr optional( T&& value ); | (5) | (since C++17) |
template< class... Args > constexpr explicit optional( std::in_place_t, Args&&... args ); | (6) | (since C++17) |
template< class U, class... Args > constexpr explicit optional( std::in_place_t, std::initializer_list<U> ilist, Args&&... args ); | (7) | (since C++17) |
Constructs a new optional
object.
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.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.T
with the expression value
. This constructor is constexpr
if the constructor of T
selected by direct-initialization is constexpr
.T
with the expression std::move(value)
. This constructor is constexpr
if the constructor of T
selected by direct-initialization is constexpr
.T
from the arguments std::forward<Args>(args)...
T
from the arguments ilist, std::forward<Args>(args)...
. The function does not participate in the overload resolution if std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value != true
other | - | another optional object whose contained value to copy |
value | - | value to initialize the contained value with |
args... | - | arguments to initialize the contained value with |
ilist | - | initializer list to initialize the contained value with |
Type requirements | ||
-
T must meet the requirements of CopyConstructible in order to use overloads (2,4). |
||
-
T must meet the requirements of MoveConstructible in order to use overloads (3,5). |
noexcept
specification: noexcept
T
.T
. Has the following noexcept
declaration: noexcept
specification: noexcept(std::is_nothrow_move_constructible<T>::value)
T
.#include <optional> #include <iostream> #include <string> int main() { std::optional<int> o1, // empty o2 = 1, // init from rvalue o3 = o2; // copy-constructor // calls string( initializer_list<CharT> ) constructor std::optional<std::string> o4(std::in_place, {'a', 'b', 'c'}); // calls string( size_type count, CharT ch ) constructor std::optional<std::string> o5(std::in_place, 3, 'A'); std::cout << *o2 << ' ' << *o3 << ' ' << *o4 << ' ' << *o5 << '\n'; }
Output:
1 1 abc AAA
(C++17)
| creates an optional object (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/optional/optional