std:: make_optional
|
ヘッダー
<optional>
で定義
|
||
|
template
<
class
T
>
constexpr std:: optional < std:: decay_t < T >> make_optional ( T && value ) ; |
(1) | (C++17以降) |
|
template
<
class
T,
class
...
Args
>
constexpr std:: optional < T > make_optional ( Args && ... args ) ; |
(2) | (C++17以降) |
|
template
<
class
T,
class
U,
class
...
Args
>
constexpr
std::
optional
<
T
>
make_optional
(
std::
initializer_list
<
U
>
il,
|
(3) | (C++17以降) |
このオーバーロードは、 std:: is_constructible_v < T, Args... > が true の場合にのみオーバーロード解決に参加します。
このオーバーロードは、 std:: is_constructible_v < T, std:: initializer_list < U > & , Args... > が true の場合にのみオーバーロード解決に参加します。
目次 |
パラメータ
| value | - | オプショナルオブジェクトを構築する値 |
| il, args | - |
T
のコンストラクタに渡される引数
|
戻り値
構築されたオプショナルオブジェクト。
例外
T
のコンストラクタによってスローされるあらゆる例外をスローします。
注記
T
は、保証されたコピー省略により、オーバーロード
(
2,3
)
では movable である必要はありません。
例
#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'; }
出力例:
op1: a,b,c, op2: 2,2,2,2,2, op3: "hello world" str: ""
関連項目
optional
オブジェクトを構築する
(公開メンバ関数) |