From 0ec78a936934332735d99894aeedc688aeb89fca Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 7 Aug 2018 20:13:26 +0100 Subject: [PATCH] PR libstdc++/86874 fix std::variant::swap regression PR libstdc++/86874 * include/std/variant (_Copy_ctor_base::_M_destructive_move): Define here instead of in _Move_assign_base. (_Copy_ctor_base::_M_destructive_move): Define. (_Copy_assign_base::operator=): Use _M_destructive_move when changing the contained value to another alternative. (_Move_assign_base::operator=): Likewise. (_Move_assign_base::_M_destructive_move): Remove. * testsuite/20_util/variant/86874.cc: New test. From-SVN: r263365 --- libstdc++-v3/ChangeLog | 10 ++++ libstdc++-v3/include/std/variant | 59 ++++++++----------- .../testsuite/20_util/variant/86874.cc | 55 +++++++++++++++++ 3 files changed, 89 insertions(+), 35 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/variant/86874.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ce410ed2e0d..50a01efd77f 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,15 @@ 2018-08-07 Jonathan Wakely + PR libstdc++/86874 + * include/std/variant (_Copy_ctor_base::_M_destructive_move): Define + here instead of in _Move_assign_base. + (_Copy_ctor_base::_M_destructive_move): Define. + (_Copy_assign_base::operator=): Use _M_destructive_move when changing + the contained value to another alternative. + (_Move_assign_base::operator=): Likewise. + (_Move_assign_base::_M_destructive_move): Remove. + * testsuite/20_util/variant/86874.cc: New test. + PR libstdc++/86861 * libsupc++/new_opa.cc [_GLIBCXX_HAVE_MEMALIGN] (aligned_alloc): Replace macro with inline function. diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 66d878142a4..2d86a704c63 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -506,6 +506,20 @@ namespace __variant } } + void _M_destructive_move(_Move_ctor_base&& __rhs) + { + this->~_Move_ctor_base(); + __try + { + ::new (this) _Move_ctor_base(std::move(__rhs)); + } + __catch (...) + { + this->_M_index = variant_npos; + __throw_exception_again; + } + } + _Move_ctor_base(const _Move_ctor_base&) = default; _Move_ctor_base& operator=(const _Move_ctor_base&) = default; _Move_ctor_base& operator=(_Move_ctor_base&&) = default; @@ -516,6 +530,12 @@ namespace __variant { using _Base = _Copy_ctor_alias<_Types...>; using _Base::_Base; + + void _M_destructive_move(_Move_ctor_base&& __rhs) + { + this->~_Move_ctor_base(); + ::new (this) _Move_ctor_base(std::move(__rhs)); + } }; template @@ -538,22 +558,14 @@ namespace __variant { static constexpr void (*_S_vtable[])(void*, void*) = { &__erased_assign<_Types&, const _Types&>... }; - _S_vtable[__rhs._M_index](this->_M_storage(), __rhs._M_storage()); + _S_vtable[__rhs._M_index](this->_M_storage(), + __rhs._M_storage()); } } else { _Copy_assign_base __tmp(__rhs); - this->~_Copy_assign_base(); - __try - { - ::new (this) _Copy_assign_base(std::move(__tmp)); - } - __catch (...) - { - this->_M_index = variant_npos; - __throw_exception_again; - } + this->_M_destructive_move(std::move(__tmp)); } __glibcxx_assert(this->_M_index == __rhs._M_index); return *this; @@ -582,20 +594,6 @@ namespace __variant using _Base = _Copy_assign_alias<_Types...>; using _Base::_Base; - void _M_destructive_move(_Move_assign_base&& __rhs) - { - this->~_Move_assign_base(); - __try - { - ::new (this) _Move_assign_base(std::move(__rhs)); - } - __catch (...) - { - this->_M_index = variant_npos; - __throw_exception_again; - } - } - _Move_assign_base& operator=(_Move_assign_base&& __rhs) noexcept(_Traits<_Types...>::_S_nothrow_move_assign) @@ -613,16 +611,7 @@ namespace __variant else { _Move_assign_base __tmp(std::move(__rhs)); - this->~_Move_assign_base(); - __try - { - ::new (this) _Move_assign_base(std::move(__tmp)); - } - __catch (...) - { - this->_M_index = variant_npos; - __throw_exception_again; - } + this->_M_destructive_move(std::move(__tmp)); } __glibcxx_assert(this->_M_index == __rhs._M_index); return *this; diff --git a/libstdc++-v3/testsuite/20_util/variant/86874.cc b/libstdc++-v3/testsuite/20_util/variant/86874.cc new file mode 100644 index 00000000000..b595f9581a1 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/variant/86874.cc @@ -0,0 +1,55 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +#include +#include + +void +test01() +{ + std::variant v1, v2; + std::swap(v1, v2); +} + +void +test02() +{ + std::variant v1{1}, v2{2}; + std::swap(v1, v2); + VERIFY( std::get<0>(v1) == 2 ); + VERIFY( std::get<0>(v2) == 1 ); +} + +void +test03() +{ + std::variant v1{1}, v2{2.3}; + std::swap(v1, v2); + VERIFY( std::get(v1) == 2.3 ); + VERIFY( std::get(v2) == 1 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} -- 2.30.2