From: Yuao Ma Date: Thu, 11 Dec 2025 14:24:42 +0000 (+0800) Subject: libstdc++: constexpr flat_set and flat_multiset X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bf9dd44a97400edbc9846dc093253c9c483a5ed8;p=thirdparty%2Fgcc.git libstdc++: constexpr flat_set and flat_multiset This patch makes flat_set and flat_multiset constexpr as part of P3372R3. libstdc++-v3/ChangeLog: * include/bits/version.def: Add FTM. * include/bits/version.h: Regenerate. * include/std/flat_set: Add constexpr. * testsuite/23_containers/flat_multiset/1.cc: Add constexpr test. * testsuite/23_containers/flat_set/1.cc: Add constexpr test. --- diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index 5c010a4f770..ed47cc8bef8 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -1351,6 +1351,14 @@ ftms = { }; }; +ftms = { + name = constexpr_flat_set; + values = { + v = 202502; + cxxmin = 26; + }; +}; + ftms = { name = constexpr_string; values = { diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index 26d0689294a..a3125b65b35 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -1496,6 +1496,16 @@ #endif /* !defined(__cpp_lib_constexpr_dynamic_alloc) */ #undef __glibcxx_want_constexpr_dynamic_alloc +#if !defined(__cpp_lib_constexpr_flat_set) +# if (__cplusplus > 202302L) +# define __glibcxx_constexpr_flat_set 202502L +# if defined(__glibcxx_want_all) || defined(__glibcxx_want_constexpr_flat_set) +# define __cpp_lib_constexpr_flat_set 202502L +# endif +# endif +#endif /* !defined(__cpp_lib_constexpr_flat_set) */ +#undef __glibcxx_want_constexpr_flat_set + #if !defined(__cpp_lib_constexpr_string) # if (__cplusplus >= 202002L) && _GLIBCXX_USE_CXX11_ABI && _GLIBCXX_HOSTED && (defined(__glibcxx_is_constant_evaluated)) # define __glibcxx_constexpr_string 201907L diff --git a/libstdc++-v3/include/std/flat_set b/libstdc++-v3/include/std/flat_set index c48340d7980..3a1f5ba10da 100644 --- a/libstdc++-v3/include/std/flat_set +++ b/libstdc++-v3/include/std/flat_set @@ -33,6 +33,7 @@ #pragma GCC system_header #endif +#define __glibcxx_want_constexpr_flat_set #define __glibcxx_want_flat_set #include @@ -100,50 +101,60 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { container_type* _M_cont; + _GLIBCXX26_CONSTEXPR _ClearGuard(container_type& __cont) : _M_cont(std::__addressof(__cont)) { } + _GLIBCXX26_CONSTEXPR ~_ClearGuard() { if (_M_cont) _M_cont->clear(); } + _GLIBCXX26_CONSTEXPR void _M_disable() { _M_cont = nullptr; } }; + _GLIBCXX26_CONSTEXPR _ClearGuard _M_make_clear_guard() { return _ClearGuard{this->_M_cont}; } public: // constructors + _GLIBCXX26_CONSTEXPR _Flat_set_impl() : _Flat_set_impl(key_compare()) { } + _GLIBCXX26_CONSTEXPR explicit _Flat_set_impl(const key_compare& __comp) : _M_cont(), _M_comp(__comp) { } + _GLIBCXX26_CONSTEXPR _Flat_set_impl(container_type __cont, const key_compare& __comp = key_compare()) : _M_cont(std::move(__cont)), _M_comp(__comp) { _M_sort_uniq(); } + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t, container_type __cont, const key_compare& __comp = key_compare()) : _M_cont(std::move(__cont)), _M_comp(__comp) { _GLIBCXX_DEBUG_ASSERT(ranges::is_sorted(_M_cont, _M_comp)); } template<__has_input_iter_cat _InputIterator> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(_InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare()) : _M_cont(), _M_comp(__comp) { insert(__first, __last); } template<__has_input_iter_cat _InputIterator> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t __s, _InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare()) @@ -151,20 +162,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { insert(__s, __first, __last); } template<__detail::__container_compatible_range _Rg> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(from_range_t, _Rg&& __rg) : _Flat_set_impl(from_range, std::forward<_Rg>(__rg), key_compare()) { } template<__detail::__container_compatible_range _Rg> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(from_range_t, _Rg&& __rg, const key_compare& __comp) : _Flat_set_impl(__comp) { insert_range(std::forward<_Rg>(__rg)); } + _GLIBCXX26_CONSTEXPR _Flat_set_impl(initializer_list __il, const key_compare& __comp = key_compare()) : _Flat_set_impl(__il.begin(), __il.end(), __comp) { } + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t __s, initializer_list __il, const key_compare& __comp = key_compare()) @@ -174,23 +189,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // constructors with allocators template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR explicit _Flat_set_impl(const _Alloc& __a) : _Flat_set_impl(key_compare(), __a) { } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(const key_compare& __comp, const _Alloc& __a) : _M_cont(std::make_obj_using_allocator(__a)), _M_comp(__comp) { } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(const container_type& __cont, const _Alloc& __a) : _Flat_set_impl(__cont, key_compare(), __a) { } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(const container_type& __cont, const key_compare& __comp, const _Alloc& __a) : _M_cont(std::make_obj_using_allocator(__a, __cont)), @@ -198,11 +217,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { _M_sort_uniq(); } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t __s, const container_type& __cont, const _Alloc& __a) : _Flat_set_impl(__s, __cont, key_compare(), __a) { } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t, const container_type& __cont, const key_compare& __comp, const _Alloc& __a) : _M_cont(std::make_obj_using_allocator(__a, __cont)), @@ -210,24 +231,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { _GLIBCXX_DEBUG_ASSERT(ranges::is_sorted(_M_cont, _M_comp)); } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(const _Derived& __x, const _Alloc& __a) : _M_cont(std::make_obj_using_allocator(__a, __x._M_cont)), _M_comp(__x._M_comp) { } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(_Derived&& __x, const _Alloc& __a) : _M_cont(std::make_obj_using_allocator(__a, std::move(__x._M_cont))), _M_comp(__x._M_comp) { } template<__has_input_iter_cat _InputIterator, __allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(_InputIterator __first, _InputIterator __last, const _Alloc& __a) : _Flat_set_impl(std::move(__first), std::move(__last), key_compare(), __a) { } template<__has_input_iter_cat _InputIterator, __allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(_InputIterator __first, _InputIterator __last, const key_compare& __comp, const _Alloc& __a) @@ -235,6 +260,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { insert(__first, __last); } template<__has_input_iter_cat _InputIterator, __allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t __s, _InputIterator __first, _InputIterator __last, const _Alloc& __a) @@ -242,6 +268,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { } template<__has_input_iter_cat _InputIterator, __allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t __s, _InputIterator __first, _InputIterator __last, const key_compare& __comp, @@ -251,6 +278,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template<__detail::__container_compatible_range _Rg, __allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(from_range_t, _Rg&& __rg, const _Alloc& __a) : _Flat_set_impl(from_range, std::forward<_Rg>(__rg), key_compare(), __a) @@ -258,6 +286,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template<__detail::__container_compatible_range _Rg, __allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(from_range_t, _Rg&& __rg, const key_compare& __comp, const _Alloc& __a) @@ -265,12 +294,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { insert_range(std::forward<_Rg>(__rg)); } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(initializer_list __il, const _Alloc& __a) : _Flat_set_impl(__il, key_compare(), __a) { } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(initializer_list __il, const key_compare& __comp, const _Alloc& __a) @@ -278,6 +309,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t __s, initializer_list __il, const _Alloc& __a) @@ -285,6 +317,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { } template<__allocator_for _Alloc> + _GLIBCXX26_CONSTEXPR _Flat_set_impl(__sorted_t __s, initializer_list __il, const key_compare& __comp, @@ -292,6 +325,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _Flat_set_impl(__s, __il.begin(), __il.end(), __comp, __a) { } + _GLIBCXX26_CONSTEXPR _Derived& operator=(initializer_list __il) { @@ -303,54 +337,66 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } // iterators + _GLIBCXX26_CONSTEXPR const_iterator begin() const noexcept { return _M_cont.begin(); } + _GLIBCXX26_CONSTEXPR const_iterator end() const noexcept { return _M_cont.end(); } + _GLIBCXX26_CONSTEXPR const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } + _GLIBCXX26_CONSTEXPR const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } + _GLIBCXX26_CONSTEXPR const_iterator cbegin() const noexcept { return begin(); } + _GLIBCXX26_CONSTEXPR const_iterator cend() const noexcept { return end(); } + _GLIBCXX26_CONSTEXPR const_reverse_iterator crbegin() const noexcept { return rbegin(); } + _GLIBCXX26_CONSTEXPR const_reverse_iterator crend() const noexcept { return rend(); } // capacity [[nodiscard]] + _GLIBCXX26_CONSTEXPR bool empty() const noexcept { return _M_cont.empty(); } + _GLIBCXX26_CONSTEXPR size_type size() const noexcept { return _M_cont.size(); } + _GLIBCXX26_CONSTEXPR size_type max_size() const noexcept { return _M_cont.max_size(); } // modifiers template + _GLIBCXX26_CONSTEXPR pair _M_try_emplace(optional __hint, _Arg&& __arg, _Args&&... __args) { @@ -410,12 +456,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template + _GLIBCXX26_CONSTEXPR pair _M_try_emplace(optional __hint) { return _M_try_emplace(__hint, value_type()); } template requires is_constructible_v + _GLIBCXX26_CONSTEXPR __emplace_result_t emplace(_Args&&... __args) { @@ -427,39 +475,47 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template + _GLIBCXX26_CONSTEXPR iterator emplace_hint(const_iterator __position, _Args&&... __args) { return _M_try_emplace(__position, std::forward<_Args>(__args)...).first; } + _GLIBCXX26_CONSTEXPR __emplace_result_t insert(const value_type& __x) { return emplace(__x); } + _GLIBCXX26_CONSTEXPR __emplace_result_t insert(value_type&& __x) { return emplace(std::move(__x)); } + _GLIBCXX26_CONSTEXPR iterator insert(const_iterator __position, const value_type& __x) { return emplace_hint(__position, __x); } + _GLIBCXX26_CONSTEXPR iterator insert(const_iterator __position, value_type&& __x) { return emplace_hint(__position, std::move(__x)); } template requires is_constructible_v + _GLIBCXX26_CONSTEXPR __emplace_result_t insert(_Arg&& __x) { return emplace(std::forward<_Arg>(__x)); } template requires is_constructible_v + _GLIBCXX26_CONSTEXPR iterator insert(const_iterator __position, _Arg&& __x) { return emplace_hint(__position, std::forward<_Arg>(__x)); } template<__has_input_iter_cat _InputIterator> + _GLIBCXX26_CONSTEXPR void insert(_InputIterator __first, _InputIterator __last) { @@ -473,6 +529,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template<__has_input_iter_cat _InputIterator> + _GLIBCXX26_CONSTEXPR void insert(__sorted_t, _InputIterator __first, _InputIterator __last) { @@ -485,6 +542,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template<__detail::__container_compatible_range _Rg> + _GLIBCXX26_CONSTEXPR void insert_range(_Rg&& __rg) { @@ -511,14 +569,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __guard._M_disable(); } + _GLIBCXX26_CONSTEXPR void insert(initializer_list __il) { insert(__il.begin(), __il.end()); } + _GLIBCXX26_CONSTEXPR void insert(__sorted_t __s, initializer_list __il) { insert(__s, __il.begin(), __il.end()); } + _GLIBCXX26_CONSTEXPR container_type extract() && { @@ -526,6 +587,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return std::move(_M_cont); } + _GLIBCXX26_CONSTEXPR void replace(container_type&& __cont) { @@ -535,10 +597,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __guard._M_disable(); } + _GLIBCXX26_CONSTEXPR iterator erase(const_iterator __position) { return _M_cont.erase(__position); } + _GLIBCXX26_CONSTEXPR size_type erase(const key_type& __x) { return erase(__x); } @@ -548,6 +612,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION || (__transparent_comparator<_Compare> && !is_convertible_v<_Key2, iterator> && !is_convertible_v<_Key2, const_iterator>) + _GLIBCXX26_CONSTEXPR size_type erase(_Key2&& __x) { @@ -557,10 +622,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __n; } + _GLIBCXX26_CONSTEXPR iterator erase(const_iterator __first, const_iterator __last) { return _M_cont.erase(__first, __last); } + _GLIBCXX26_CONSTEXPR void swap(_Derived& __x) noexcept { @@ -569,28 +636,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION swap(_M_comp, __x._M_comp); } + _GLIBCXX26_CONSTEXPR void clear() noexcept { _M_cont.clear(); } // observers [[nodiscard]] + _GLIBCXX26_CONSTEXPR key_compare key_comp() const { return _M_comp; } [[nodiscard]] + _GLIBCXX26_CONSTEXPR value_compare value_comp() const { return _M_comp; } // set operations [[nodiscard]] + _GLIBCXX26_CONSTEXPR iterator find(const key_type& __x) { return find(__x); } [[nodiscard]] + _GLIBCXX26_CONSTEXPR const_iterator find(const key_type& __x) const { return find(__x); } @@ -598,6 +670,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR iterator find(const _Key2& __x) { @@ -611,6 +684,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR const_iterator find(const _Key2& __x) const { @@ -622,6 +696,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } [[nodiscard]] + _GLIBCXX26_CONSTEXPR size_type count(const key_type& __x) const { return count(__x); } @@ -629,6 +704,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR size_type count(const _Key2& __x) const { @@ -642,6 +718,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } [[nodiscard]] + _GLIBCXX26_CONSTEXPR bool contains(const key_type& __x) const { return contains(__x); } @@ -649,16 +726,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR bool contains(const _Key2& __x) const { return find(__x) != cend(); } [[nodiscard]] + _GLIBCXX26_CONSTEXPR iterator lower_bound(const key_type& __x) { return lower_bound(__x); } [[nodiscard]] + _GLIBCXX26_CONSTEXPR const_iterator lower_bound(const key_type& __x) const { return lower_bound(__x); } @@ -666,6 +746,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR iterator lower_bound(const _Key2& __x) { return std::lower_bound(begin(), end(), __x, _M_comp); } @@ -673,16 +754,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR const_iterator lower_bound(const _Key2& __x) const { return std::lower_bound(begin(), end(), __x, _M_comp); } [[nodiscard]] + _GLIBCXX26_CONSTEXPR iterator upper_bound(const key_type& __x) { return upper_bound(__x); } [[nodiscard]] + _GLIBCXX26_CONSTEXPR const_iterator upper_bound(const key_type& __x) const { return upper_bound(__x); } @@ -690,6 +774,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR iterator upper_bound(const _Key2& __x) { return std::upper_bound(begin(), end(), __x, _M_comp); } @@ -697,16 +782,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR const_iterator upper_bound(const _Key2& __x) const { return std::upper_bound(begin(), end(), __x, _M_comp); } [[nodiscard]] + _GLIBCXX26_CONSTEXPR pair equal_range(const key_type& __x) { return equal_range(__x); } [[nodiscard]] + _GLIBCXX26_CONSTEXPR pair equal_range(const key_type& __x) const { return equal_range(__x); } @@ -714,6 +802,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR pair equal_range(const _Key2& __x) { return std::equal_range(begin(), end(), __x, _M_comp); } @@ -721,18 +810,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires same_as<_Key2, _Key> || __transparent_comparator<_Compare> [[nodiscard]] + _GLIBCXX26_CONSTEXPR pair equal_range(const _Key2& __x) const { return std::equal_range(begin(), end(), __x, _M_comp); } [[nodiscard]] - friend bool + friend _GLIBCXX26_CONSTEXPR bool operator==(const _Derived& __x, const _Derived& __y) { return std::equal(__x.begin(), __x.end(), __y.begin(), __y.end()); } template [[nodiscard]] - friend __detail::__synth3way_t<_Up> + friend _GLIBCXX26_CONSTEXPR __detail::__synth3way_t<_Up> operator<=>(const _Derived& __x, const _Derived& __y) { return std::lexicographical_compare_three_way(__x.begin(), __x.end(), @@ -740,11 +830,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __detail::__synth3way); } - friend void + friend _GLIBCXX26_CONSTEXPR void swap(_Derived& __x, _Derived& __y) noexcept { return __x.swap(__y); } template + _GLIBCXX26_CONSTEXPR size_type _M_erase_if(_Predicate __pred) { @@ -762,6 +853,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION container_type _M_cont; [[no_unique_address]] _Compare _M_comp; + _GLIBCXX26_CONSTEXPR void _M_sort_uniq() { @@ -770,13 +862,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_unique(); } + _GLIBCXX26_CONSTEXPR void _M_unique() requires (!_Multi) { struct __key_equiv { + _GLIBCXX26_CONSTEXPR __key_equiv(key_compare __c) : _M_comp(__c) { } + _GLIBCXX26_CONSTEXPR bool operator()(const_reference __x, const_reference __y) const { return !_M_comp(__x, __y) && !_M_comp(__y, __x); } @@ -934,6 +1029,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template + _GLIBCXX26_CONSTEXPR typename flat_set<_Key, _Compare, _KeyContainer>::size_type erase_if(flat_set<_Key, _Compare, _KeyContainer>& __c, _Predicate __pred) { return __c._M_erase_if(std::move(__pred)); } @@ -1081,6 +1177,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template + _GLIBCXX26_CONSTEXPR typename flat_multiset<_Key, _Compare, _KeyContainer>::size_type erase_if(flat_multiset<_Key, _Compare, _KeyContainer>& __c, _Predicate __pred) { return __c._M_erase_if(std::move(__pred)); } diff --git a/libstdc++-v3/testsuite/23_containers/flat_multiset/1.cc b/libstdc++-v3/testsuite/23_containers/flat_multiset/1.cc index 63855e07065..be330fefdd0 100644 --- a/libstdc++-v3/testsuite/23_containers/flat_multiset/1.cc +++ b/libstdc++-v3/testsuite/23_containers/flat_multiset/1.cc @@ -8,6 +8,7 @@ #include template class Sequence> +constexpr void test01() { @@ -42,6 +43,7 @@ test01() VERIFY( m.size() == 5 ); } +constexpr void test02() { @@ -67,6 +69,7 @@ test02() VERIFY( m.count(3) == 2 ); } +constexpr void test03() { @@ -95,6 +98,7 @@ test03() VERIFY( std::ranges::equal(m, (int[]){5}) ); } +constexpr void test04() { @@ -121,6 +125,7 @@ test04() VERIFY( std::move(m5).extract().get_allocator().get_personality() == 44 ); } +constexpr void test05() { @@ -129,6 +134,7 @@ test05() VERIFY( std::ranges::equal(m, (int[]){1, 2, 3, 3, 4, 5}) ); } +constexpr void test06() { @@ -156,25 +162,25 @@ struct NoCatIterator { using difference_type = int; using value_type = int; - NoCatIterator() : v(0) {} - NoCatIterator(int x) : v(x) {} + constexpr NoCatIterator() : v(0) {} + constexpr NoCatIterator(int x) : v(x) {} - int operator*() const + constexpr int operator*() const { return v; } - NoCatIterator& operator++() + constexpr NoCatIterator& operator++() { ++v; return *this; } - NoCatIterator operator++(int) + constexpr NoCatIterator operator++(int) { ++v; return NoCatIterator(v-1); } - bool operator==(const NoCatIterator& rhs) const + constexpr bool operator==(const NoCatIterator& rhs) const { return v == rhs.v; } private: @@ -189,7 +195,9 @@ struct std::iterator_traits { // no iterator_category, happens also for common_iterator }; -void test07() +constexpr +void +test07() { std::flat_multiset s; std::flat_multiset, NoInsertRange> s2; @@ -214,27 +222,39 @@ void test07() #endif } +constexpr void test08() { // PR libstdc++/119620 -- flat_set::emplace always constructs element on the stack - static int copy_counter; + int copy_counter = 0; + struct A { - A() { } - A(const A&) { ++copy_counter; } - A& operator=(const A&) { ++copy_counter; return *this; } - auto operator<=>(const A&) const = default; + int *counter; + constexpr A(int &c) : counter(&c) {} + + constexpr A(const A &other) : counter(other.counter) { ++(*counter); } + + constexpr A &operator=(const A &other) { + counter = other.counter; + ++(*counter); + return *this; + } + + constexpr auto operator<=>(const A &) const = default; }; + std::vector v; v.reserve(2); std::flat_multiset s(std::move(v)); - A a; + A a(copy_counter); s.emplace(a); VERIFY( copy_counter == 1 ); s.emplace(a); VERIFY( copy_counter == 2 ); } +constexpr void test09() { @@ -245,8 +265,8 @@ test09() VERIFY( std::ranges::equal(s, (int[]){2,2,4}) ); } -int -main() +void +test() { test01(); test01(); @@ -259,3 +279,30 @@ main() test08(); test09(); } + +constexpr +bool +test_constexpr() +{ + test01(); + test02(); + test03(); + test04(); + test06(); + test07(); + test08(); + test09(); + return true; +} + +int +main() +{ + test(); +#if __cplusplus > 202302L + static_assert(test_constexpr()); +#if __cpp_lib_constexpr_flat_set != 202502L +#error "Feature-test macro __cpp_lib_constexpr_flat_set has wrong value in " +#endif +#endif +} diff --git a/libstdc++-v3/testsuite/23_containers/flat_set/1.cc b/libstdc++-v3/testsuite/23_containers/flat_set/1.cc index b1d9002caba..a14d03476e1 100644 --- a/libstdc++-v3/testsuite/23_containers/flat_set/1.cc +++ b/libstdc++-v3/testsuite/23_containers/flat_set/1.cc @@ -13,6 +13,7 @@ #include template class KeyContainer> +constexpr void test01() { @@ -56,6 +57,7 @@ test01() VERIFY( m_copy == m ); } +constexpr void test02() { @@ -82,6 +84,7 @@ test02() VERIFY( m.count(3) == 1 ); } +constexpr void test03() { @@ -110,6 +113,7 @@ test03() VERIFY( std::ranges::equal(m, (int[]){5}) ); } +constexpr void test04() { @@ -136,6 +140,7 @@ test04() VERIFY( std::move(m5).extract().get_allocator().get_personality() == 44 ); } +constexpr void test05() { @@ -144,6 +149,7 @@ test05() VERIFY( std::ranges::equal(m, (int[]){1, 2, 3, 4, 5}) ); } +constexpr void test06() { @@ -171,25 +177,25 @@ struct NoCatIterator { using difference_type = int; using value_type = int; - NoCatIterator() : v(0) {} - NoCatIterator(int x) : v(x) {} + constexpr NoCatIterator() : v(0) {} + constexpr NoCatIterator(int x) : v(x) {} - int operator*() const + constexpr int operator*() const { return v; } - NoCatIterator& operator++() + constexpr NoCatIterator& operator++() { ++v; return *this; } - NoCatIterator operator++(int) + constexpr NoCatIterator operator++(int) { ++v; return NoCatIterator(v-1); } - bool operator==(const NoCatIterator& rhs) const + constexpr bool operator==(const NoCatIterator& rhs) const { return v == rhs.v; } private: @@ -204,7 +210,9 @@ struct std::iterator_traits { // no iterator_category, happens also for common_iterator }; -void test07() +constexpr +void +test07() { std::flat_set s; std::flat_set, NoInsertRange> s2; @@ -229,25 +237,36 @@ void test07() #endif } +constexpr void test08() { // PR libstdc++/119620 -- flat_set::emplace always constructs element on the stack - static int copy_counter; + int copy_counter = 0; + struct A { - A() { } - A(const A&) { ++copy_counter; } - A& operator=(const A&) { ++copy_counter; return *this; } - auto operator<=>(const A&) const = default; + int *counter; + constexpr A(int &c) : counter(&c) {} + + constexpr A(const A &other) : counter(other.counter) { ++(*counter); } + + constexpr A &operator=(const A &other) { + counter = other.counter; + ++(*counter); + return *this; + } + + constexpr auto operator<=>(const A &) const = default; }; std::flat_set s; - A a; + A a(copy_counter); s.emplace(a); VERIFY( copy_counter == 1 ); s.emplace(a); VERIFY( copy_counter == 1 ); } +constexpr void test09() { @@ -258,8 +277,8 @@ test09() VERIFY( std::ranges::equal(s, (int[]){2,4}) ); } -int -main() +void +test() { test01(); test01(); @@ -272,3 +291,30 @@ main() test08(); test09(); } + +constexpr +bool +test_constexpr() +{ + test01(); + test02(); + test03(); + test04(); + test06(); + test07(); + test08(); + test09(); + return true; +} + +int +main() +{ + test(); +#if __cplusplus > 202302L + static_assert(test_constexpr()); +#if __cpp_lib_constexpr_flat_set != 202502L +#error "Feature-test macro __cpp_lib_constexpr_flat_set has wrong value in " +#endif +#endif +}