Why is std::nullopt_t part of the C++ standard?

前端 未结 2 690
一生所求
一生所求 2021-01-13 18:14

I don\'t understand the reasoning for the inclusion of std::nullopt_t in the standard. Does it exist strictly for convenience, or is it required in some niche c

相关标签:
2条回答
  • 2021-01-13 18:31

    C++ reference says it all:

    std::nullopt_t is an empty class type used to indicate optional type with uninitialized state. In particular, std::optional has a constructor with nullopt_t as a single argument, which creates an optional that does not contain a value.

    std::nullopt_t

    0 讨论(0)
  • 2021-01-13 18:47

    nullopt_t is the type of nullopt which indicates disengaged optional state. nullopt allows disambiguating overloads such as (example from the optional proposal):

    void run(complex<double> v);
    void run(optional<string> v);
    
    run(nullopt);              // pick the second overload
    run({});                   // ambiguous
    
    0 讨论(0)
提交回复
热议问题