Search code examples
c++c++17stdoptional

How would a std::optional<std::nullopt_t> operate?


Kind of a silly question, but I'm curious and I haven't found this explained. Is it legal to construct a std::optional<std::nullopt_t>? If you did, what would

std::optional<std::nullopt_t> x(std::nullopt);

do? Would x contain a value or not?


Solution

  • As pointed out in the comments, instantiating a optional with nullopt_t (or in_place_t) is explicitly forbidden and will fail with a compile error.