Namespaces
Variants
Actions

deduction guides for std::optional

From cppreference.com
< cpp‎ | utility‎ | optional
 
C++
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
std::optional
Defined in header <optional>
template< class T >
optional(T) -> optional<T>;
(since C++17)

One deduction guide is provided for std::optional to account for the edge cases missed by the implicit deduction guides, in particular, non-copyable arguments and array to pointer conversion.

[edit] Example

#include <optional>
#include <type_traits>
 
int main()
{
    int a[2];
    std::optional oa{a}; / uses explicit deduction guide
    static_assert(std::is_same_v<decltype(oa), std::optional<int*>> == true);
}

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant