Should std::nullopt_t's constructor be required to be explicit?

472 views
Skip to first unread message

ndkr...@gmail.com

unread,
Mar 5, 2019, 11:28:27 AM3/5/19
to ISO C++ Standard - Discussion
If std::nullopt_t's constructor is explicit, the following snippet has the effect of assigning a default constructed Foo to an optional:

struct Foo { /* ... */ };
std::optional<Foo> opt;
opt = {{}};

If the std::nullopt_t constructor is not explicit, it seems to be ambiguous between operator=(std::nullopt_t) and operator=(optional&&) - with the the std::nullopt_t constructor explicit, it is unambiguously operator=(optional&&), which in turn uses the "template <typename U = value_type> constexpr optional(U&& value)" constructor.

(At least this is the clang 7 interpretation - gcc 8 fails regardless of "explicit". I'm not sure the standard interpretation?)

Reply all
Reply to author
Forward
0 new messages