constexpr std::optional reset

  • A+
Category:Languages

I was reviewing the interface for the C++-17 std::optional class template and noticed that the reset and assignment from nullopt are not marked as constexpr.

Was this an oversight or is there a reason that this operation cannot be marked constexpr?

 


There was a reason, which was that [expr.const] had previously forbid:

an assignment expression or invocation of an assignment operator ([class.copy]) that would change the active member of a union;

That restriction no longer exists as a result of P1330: Changing the active member of a union inside constexpr, which makes all of these things much easier to implement (the paper literally just removes the bullet point I quoted above).

The reason that optional's copy and move assignment (but none of the other assignments) were constexpr was because they can just be defaulted for trivial types.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: