PR libstdc++/87431 fix regression introduced by r264574
commitcaf80d87b53402b64eee8bba2cf5b036c43bf60b
authorJonathan Wakely <jwakely@redhat.com>
Sun, 6 Jan 2019 20:52:34 +0000 (6 20:52 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Sun, 6 Jan 2019 20:52:34 +0000 (6 20:52 +0000)
treef24b48f30f75133c6feccb12ea6e56b6481a0cc2
parentbe917808aa3310d46f586e6586ef08d302837f1e
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.

From-SVN: r267614
libstdc++-v3/ChangeLog
libstdc++-v3/include/std/variant
libstdc++-v3/testsuite/20_util/variant/87431.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/variant/run.cc