https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87431

--- Comment #14 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Author: redi
Date: Sun Jan  6 20:52:34 2019
New Revision: 267614

URL: https://gcc.gnu.org/viewcvs?rev=267614&root=gcc&view=rev
Log:
PR libstdc++/87431 fix regression introduced by r264574

The previous patch for PR 87431 assumed that initialing a scalar type
could not throw, but it can obtain its value via a conversion operator,
which could throw. This meant the variant could get into a valueless
state, but the valueless_by_exception() member function would always
return false.

This patch fixes it by changing the emplace members to have strong
exception safety when initializing a contained value of trivially
copyable type. The _M_valid() member gets a corresponding change to
always return true for trivially copyable types, not just scalar types.

Strong exception safety (i.e. never becoming valueless) is achieved by
only replacing the current contained value once any potentially throwing
operations have completed. If constructing the new contained value can
throw then a new std::variant object is constructed to hold it, and then
move-assigned to *this (which won't throw).

        PR libstdc++/87431
        * include/std/variant (_Variant_storage<true, _Types...>::_M_valid):
        Check is_trivially_copyable instead of is_scalar.
        (variant::emplace<N, Args>(Args&&...)): If construction of the new
        contained value can throw and its type is trivially copyable then
        construct into a temporary variant and move from it, to provide the
        strong exception safety guarantee.
        (variant::emplace<N, U, Args>(initializer_list<U>, Args&&...)):
        Likewise.
        * testsuite/20_util/variant/87431.cc: New test.
        * testsuite/20_util/variant/run.cc: Adjust test so that throwing
        conversion causes valueless state.

Added:
    trunk/libstdc++-v3/testsuite/20_util/variant/87431.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/std/variant
    trunk/libstdc++-v3/testsuite/20_util/variant/run.cc

Reply via email to