Why does std::optional::operator=(U&&) require U to be a non-scalar type?

Viewed 653

For optional's template<class U = T> optional<T>& operator=(U&& v); the standard demands that (see [optional.assign]/3.16):

This function shall not participate in overload resolution unless ... conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>> is false ...

Why do we have to exclude case when assigning a scalar of type U == T?

1 Answers

This exists to support:

optional<int> o(42);
o = {}; // <== we want this to reset o

We have a bunch of assignment overloads, which take:

  1. nullopt_t
  2. optional const&
  3. optional&&
  4. U&&
  5. optional<U> const&
  6. optional<U>&&

For scalars, specifically, #4 would be a standard conversion whereas anything else would be a user-defined conversion - so it would be the best match. However, the result of that would be assigning o to be engaged with a value of 0. That would mean that o = {} could potentially mean different things depending on the type of T. Hence, we exclude scalars.

For non-scalars, #4 and #3 would be equivalent (both user-defined conversions), and #3 would win by being a non-template. No problem there.

Related