caf80d87b5
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. From-SVN: r267614
file: libstdc++-v3/README New users may wish to point their web browsers to the file index.html in the 'doc/html' subdirectory. It contains brief building instructions and notes on how to configure the library in interesting ways.