From bd9aa57612c36eb50ebc6260fd528ec085c4dc56 Mon Sep 17 00:00:00 2001 From: Jelle Date: Sat, 27 Jan 2024 17:17:53 +0000 Subject: [PATCH 1/3] `` Fix compile errors --- stl/inc/flat_map | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/stl/inc/flat_map b/stl/inc/flat_map index c73b25e9e1b..23cf6422102 100644 --- a/stl/inc/flat_map +++ b/stl/inc/flat_map @@ -757,6 +757,28 @@ protected: return _Old_size - size(); } + template + pair _Try_emplace(_KeyTy&& _Key_val, _MappedArgTypes&&... _Mapped_args) { + auto _Key_it = _STD lower_bound(_Data.keys.begin(), _Data.keys.end(), _Key_val, _Key_compare); + if (_Key_it != _Data.keys.end() && _Key_equal(*_Key_it, _STD forward<_KeyTy>(_Key_val))) { + // Already exists + return {this->begin() + _STD distance(_Data.keys.begin(), _Key_it), false}; + } else { + // Need to insert + auto _Index = _STD distance(_Data.keys.begin(), _Key_it); + this->_Insert_exact(this->cbegin() + _Index, key_type{_STD forward<_KeyTy>(_Key_val)}, + mapped_type{_STD forward<_MappedArgTypes>(_Mapped_args)...}); + return {this->begin() + _Index, true}; + } + } + + void _Insert_exact(const_iterator _Position, key_type&& _Key_val, mapped_type&& _Mapped_val) { + _Clear_flat_map_scope_guard _Guard{this}; + _Data.keys.insert(_Position._Key_it, _STD move(_Key_val)); + _Data.values.insert(_Position._Mapped_it, _STD move(_Mapped_val)); + _Guard._Clearable = nullptr; + } + private: template requires (same_as, key_type> && same_as, key_type>) @@ -792,13 +814,6 @@ private: _Guard._Clearable = nullptr; } - void _Insert_exact(const_iterator _Position, key_type&& _Key_val, mapped_type&& _Mapped_val) { - _Clear_flat_map_scope_guard _Guard{this}; - _Data.keys.insert(_Position._Key_it, _STD move(_Key_val)); - _Data.values.insert(_Position._Mapped_it, _STD move(_Mapped_val)); - _Guard._Clearable = nullptr; - } - template iterator _Emplace_hint(const_iterator _Position, _OtherKey&& _Key_val, _MappedArgTypes&&... _Args) requires is_constructible_v @@ -1161,6 +1176,7 @@ public: private: using _MyBase::_Erase_if; + using _MyBase::_Try_emplace; template friend typename flat_map<_KTy, _MTy, _Comp, _KeyCont, _MappedCont>::size_type erase_if( @@ -1190,21 +1206,6 @@ private: } } - template - pair _Try_emplace(_KeyTy&& _Key_val, _MappedArgTypes&&... _Mapped_args) { - auto _Key_it = _STD lower_bound(_Data.keys.begin(), _Data.keys.end(), _Key_val, _Key_compare); - if (_Key_it != _Data.keys.end() && _Key_equal(*_Key_it, _STD forward<_KeyTy>(_Key_val))) { - // Already exists - return {this->begin() + _STD distance(_Data.keys.begin(), _Key_it), false}; - } else { - // Need to insert - auto _Index = _STD distance(_Data.keys.begin(), _Key_it); - this->_Insert_exact(this->cbegin() + _Index, key_type{_STD forward<_KeyTy>(_Key_val)}, - mapped_type{_STD forward<_MappedArgTypes>(_Mapped_args)...}); - return {this->begin() + _Index, true}; - } - } - template pair _Insert_or_assign(_KeyTy&& _Key_val, _MappedTy&& _Mapped_val) { auto _Res = _Try_emplace(_STD forward<_KeyTy>(_Key_val), _STD forward<_MappedTy>(_Mapped_val)); @@ -1328,6 +1329,7 @@ public: private: using _MyBase::_Erase_if; + using _MyBase::_Insert_exact template friend typename flat_multimap<_KTy, _MTy, _Comp, _KeyCont, _MappedCont>::size_type erase_if( From 914ad9377f36a1149b0b0eb88cda72da2e6f1f55 Mon Sep 17 00:00:00 2001 From: Jelle Date: Sat, 27 Jan 2024 17:32:42 +0000 Subject: [PATCH 2/3] Fix format error --- stl/inc/flat_map | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/flat_map b/stl/inc/flat_map index 23cf6422102..bae20ca1f16 100644 --- a/stl/inc/flat_map +++ b/stl/inc/flat_map @@ -1329,7 +1329,7 @@ public: private: using _MyBase::_Erase_if; - using _MyBase::_Insert_exact + using _MyBase::_Insert_exact; template friend typename flat_multimap<_KTy, _MTy, _Comp, _KeyCont, _MappedCont>::size_type erase_if( From fa990595340430c5e03517a71c4d5ab1e992a6c5 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 29 Jan 2024 08:49:38 -0800 Subject: [PATCH 3/3] Add test coverage; `/permissive` rejects it. --- tests/std/tests/P0429R9_flat_map/env.lst | 2 +- tests/std/tests/P0429R9_flat_map/test.cpp | 34 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/std/tests/P0429R9_flat_map/env.lst b/tests/std/tests/P0429R9_flat_map/env.lst index 18e2d7c71ec..8ac7033b206 100644 --- a/tests/std/tests/P0429R9_flat_map/env.lst +++ b/tests/std/tests/P0429R9_flat_map/env.lst @@ -1,4 +1,4 @@ # Copyright (c) Microsoft Corporation. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -RUNALL_INCLUDE ..\concepts_latest_matrix.lst +RUNALL_INCLUDE ..\strict_concepts_latest_matrix.lst diff --git a/tests/std/tests/P0429R9_flat_map/test.cpp b/tests/std/tests/P0429R9_flat_map/test.cpp index 428855a4849..11bdfb3a8c7 100644 --- a/tests/std/tests/P0429R9_flat_map/test.cpp +++ b/tests/std/tests/P0429R9_flat_map/test.cpp @@ -300,8 +300,42 @@ namespace scary_test { static_assert(is_same_v::value_compare, flat_multimap::value_compare>); } // namespace scary_test +// GH-4344 Fix compile errors +void test_gh_4344() { + flat_map fm; + + const auto p1 = fm.try_emplace(10, 'm'); + assert(p1.first->first == 10); + assert(p1.first->second == 'm'); + assert(p1.second); + + const auto p2 = fm.try_emplace(70, 'e'); + assert(p2.first->first == 70); + assert(p2.first->second == 'e'); + assert(p2.second); + + const auto p3 = fm.try_emplace(20, 'o'); + assert(p3.first->first == 20); + assert(p3.first->second == 'o'); + assert(p3.second); + + const auto p4 = fm.try_emplace(90, 'w'); + assert(p4.first->first == 90); + assert(p4.first->second == 'w'); + assert(p4.second); + + const auto p5 = fm.try_emplace(70, 'X'); + assert(p5.first->first == 70); + assert(p5.first->second == 'e'); + assert(!p5.second); + + assert(check_key_content(fm, {10, 20, 70, 90})); + assert(check_value_content(fm, {'m', 'o', 'e', 'w'})); +} + int main() { test_construction(); test_pointer_to_incomplete_type(); test_erase_if(); + test_gh_4344(); }