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

1

16

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?

Sleuthhound answered 29/10, 2018 at 19:20 Comment(2)
probably because then another overload is fine. Looking at a single overload doesnt really give you the full pictureWillman
Types are all special cases..., but fundamental types and void in particular are very boring!Caston
J
23

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.

Juxon answered 29/10, 2018 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.