From 59e36c85e7e627feb458f2b8c43859d9422f1522 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 3 Apr 2019 10:47:51 +0100 Subject: [PATCH] PR libstdc++/85184 remove debug assertions from std::variant The __glibcxx_assert macro should be used to check preconditions that users must meet, not to check postconditions that the implementation must meet. We have tests to verify std::variant meets its postconditions, users shouldn't pay for those checks at runtime. PR libstdc++/85184 * include/std/variant (_Copy_assign_base, _Move_assign_base, variant): Remove assertions. (variant::emplace<_Tp>): Remove result of emplace directly. From-SVN: r270117 --- libstdc++-v3/ChangeLog | 5 +++++ libstdc++-v3/include/std/variant | 28 +++++++++------------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 422ec850a5d..4b0b3306b9b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2019-04-03 Jonathan Wakely + PR libstdc++/85184 + * include/std/variant (_Copy_assign_base, _Move_assign_base, variant): + Remove assertions. + (variant::emplace<_Tp>): Remove result of emplace directly. + * include/std/string (__hash_string_base): New class template defining operator() for hashing strings. (hash, hash, hash) diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index a7cc2065073..a21ef3005cf 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -612,7 +612,6 @@ namespace __variant this->_M_reset(); return {}; }, __variant_cast<_Types...>(__rhs)); - __glibcxx_assert(this->_M_index == __rhs._M_index); return *this; } @@ -670,7 +669,6 @@ namespace __variant this->_M_reset(); return {}; }, __variant_cast<_Types...>(__rhs)); - __glibcxx_assert(this->_M_index == __rhs._M_index); return *this; } @@ -1245,7 +1243,7 @@ namespace __variant noexcept(is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>) : variant(in_place_index<__accepted_index<_Tp&&>>, std::forward<_Tp>(__t)) - { __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this)); } + { } template @@ -1254,7 +1252,7 @@ namespace __variant variant(in_place_type_t<_Tp>, _Args&&... __args) : variant(in_place_index<__index_of<_Tp>>, std::forward<_Args>(__args)...) - { __glibcxx_assert(holds_alternative<_Tp>(*this)); } + { } template @@ -1265,7 +1263,7 @@ namespace __variant _Args&&... __args) : variant(in_place_index<__index_of<_Tp>>, __il, std::forward<_Args>(__args)...) - { __glibcxx_assert(holds_alternative<_Tp>(*this)); } + { } template, _Args&&... __args) : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...), _Default_ctor_enabler(_Enable_default_constructor_tag{}) - { __glibcxx_assert(index() == _Np); } + { } template, @@ -1284,7 +1282,7 @@ namespace __variant _Args&&... __args) : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...), _Default_ctor_enabler(_Enable_default_constructor_tag{}) - { __glibcxx_assert(index() == _Np); } + { } template enable_if_t<__exactly_once<__accepted_type<_Tp&&>> @@ -1300,7 +1298,6 @@ namespace __variant std::get<__index>(*this) = std::forward<_Tp>(__rhs); else this->emplace<__index>(std::forward<_Tp>(__rhs)); - __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this)); return *this; } @@ -1309,10 +1306,8 @@ namespace __variant _Tp&> emplace(_Args&&... __args) { - auto& ret = - this->emplace<__index_of<_Tp>>(std::forward<_Args>(__args)...); - __glibcxx_assert(holds_alternative<_Tp>(*this)); - return ret; + constexpr size_t __index = __index_of<_Tp>; + return this->emplace<__index>(std::forward<_Args>(__args)...); } template @@ -1321,11 +1316,8 @@ namespace __variant _Tp&> emplace(initializer_list<_Up> __il, _Args&&... __args) { - auto& ret = - this->emplace<__index_of<_Tp>>(__il, - std::forward<_Args>(__args)...); - __glibcxx_assert(holds_alternative<_Tp>(*this)); - return ret; + constexpr size_t __index = __index_of<_Tp>; + return this->emplace<__index>(__il, std::forward<_Args>(__args)...); } template @@ -1363,7 +1355,6 @@ namespace __variant this->_M_index = variant_npos; __throw_exception_again; } - __glibcxx_assert(index() == _Np); return std::get<_Np>(*this); } @@ -1399,7 +1390,6 @@ namespace __variant this->_M_index = variant_npos; __throw_exception_again; } - __glibcxx_assert(index() == _Np); return std::get<_Np>(*this); } -- 2.30.2