std::make_optional
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
std::optional
(C++26)
(C++26)
(C++23)
(C++23)
(C++23)
make_optional
Defined in header
<optional>
template< class T >
constexpr std::optional <std::decay_t <T>> make_optional( T&& value );
(1)
(since C++17)
constexpr std::optional <std::decay_t <T>> make_optional( T&& value );
template< class T, class... Args >
constexpr std::optional <T> make_optional( Args&&... args );
(2)
(since C++17)
constexpr std::optional <T> make_optional( Args&&... args );
template< class T, class U, class... Args >
(3)
(since C++17)
constexpr std::optional <T> make_optional( std::initializer_list <U> il,
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)...);.
This overload participates in overload resolution only if std::is_constructible_v <T, Args...> is true.
This overload participates in overload resolution only if std::is_constructible_v <T, Args...> is true.
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)...);.
This overload participates in overload resolution only if std::is_constructible_v <T, std::initializer_list <U>&, Args...> is true.
This overload participates in overload resolution only if std::is_constructible_v <T, std::initializer_list <U>&, 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
Run this code
#include <iomanip> #include <iostream> #include <optional> #include <string> #include <vector> int main() { auto op1 = std::make_optional<std::vector <char>>({'a','b','c'}); std::cout << "op1: "; for (char c : op1.value()) std::cout << c << ','; auto op2 = std::make_optional<std::vector <int>>(5, 2); std::cout << "\nop2: "; for (int i : *op2) std::cout << i << ','; std::string str{"hello world"}; auto op3 = std::make_optional<std::string >(std::move(str)); std::cout << "\nop3: " << std::quoted (op3.value_or("empty value")) << '\n'; std::cout << "str: " << std::quoted (str) << '\n'; }
Possible output:
op1: a,b,c, op2: 2,2,2,2,2, op3: "hello world" str: ""