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

120 Views Asked by At

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?

1

There are 1 best solutions below

1
Nicol Bolas On BEST ANSWER

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.