deduction guides for
std::optional
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::optional
| Member functions | ||||
| Observers | ||||
| Iterators | ||||
|
(C++26)
|
||||
|
(C++26)
|
||||
| Monadic operations | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
| Modifiers | ||||
| Non-member functions | ||||
| Deduction guides | ||||
| Helper classes | ||||
| Helper objects | ||||
|
ヘッダーで定義
<optional>
|
||
|
template
<
class
T
>
optional ( T ) - > optional < T > ; |
(C++17以降) | |
1つの 推論ガイド が std::optional に対して提供されており、暗黙の推論ガイドでは対応しきれないエッジケース、特にコピー不可能な引数と配列からポインタへの変換を考慮しています。
例
このコードを実行
#include <optional> #include <type_traits> int main() { int a[2]; std::optional oa{a}; // 明示的な推論ガイドを使用 static_assert(std::is_same_v<decltype(oa), std::optional<int*>> == true); }