Title
variant's move assignment should not be guaranteed to produce a valueless by exception state
Status
new
Section
[variant.assign]
Submitter
Brian Bi

Created on 2023-08-29.00:00:00 last changed 5 months ago

Messages

Date: 2023-10-30.16:39:42

Proposed resolution:

This wording is relative to N4958.

  1. Modify [variant.assign] as indicated:

    constexpr variant& operator=(variant&& rhs) noexcept(see below);
    

    -6- Let j be rhs.index().

    […]

    -8- Effects:

    1. (8.1) — If neither *this nor rhs holds a value, there is no effect.

    2. (8.2) — Otherwise, if *this holds a value but rhs does not, destroys the value contained in *this and sets *this to not hold a value.

    3. (8.3) — Otherwise, if index() == j, assigns get<j>(std::move(rhs)) to the value contained in *this.

    4. (8.4) — Otherwise, equivalent to emplace<j>(get<j>(std::move(rhs))).

    […]

    -10- Remarks: […]

    1. (10.1) — If an exception is thrown during the call to Tj's move construction (with j being rhs.index()), the variant will hold no value.

    2. (10.2) — If an exception is thrown during the call to Tj's move assignment, the state of the contained value is as defined by the exception safety guarantee of Tj's move assignment; index() will be j.

Date: 2023-10-15.00:00:00

[ 2023-10-30; Reflector poll ]

Set priority to 3 after reflector poll. "The remark is normatively redundant with the spec of emplace, strike it."

Date: 2023-08-29.00:00:00

[variant.assign] bullet 8.4 states that an alternative-changing move assignment on std::variant is equivalent to a call to emplace. However, bullet 10.1 also states that if the construction of the new alternative exits via an exception, then the destination of the assignment is guaranteed to become valueless by exception. This is inconsistent with the specification of emplace, [variant.mod]/11, which permits (but does not require) the variant to become valueless.

History
Date User Action Args
2023-10-30 16:39:42adminsetmessages: + msg13789
2023-10-03 10:45:45adminsetmessages: + msg13749
2023-08-29 00:00:00admincreate