From: Tomasz Kamiński Date: Thu, 20 Mar 2025 08:02:03 +0000 (+0100) Subject: libstdc++: Strip reference and cv-qual in range deduction guides for maps. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4794340cd257ece0d197f041812c34c5ac5d50a1;p=thirdparty%2Fgcc.git libstdc++: Strip reference and cv-qual in range deduction guides for maps. This implements part of LWG4223 that adjust the deduction guides for maps types (map, unordered_map, flat_map and non-unique equivalent) from "range" (std::from_range, iterator pair), such that referience and cv qualification are stripped from the element of the pair-like value_type. In combination with r15-8296-gd50171bc07006d, the LWG4223 is fully implemented now. libstdc++-v3/ChangeLog: * include/bits/ranges_base.h (__detail::__range_key_type): Replace remove_const_t with remove_cvref_t. (__detail::__range_mapped_type): Apply remove_cvref_t. * include/bits/stl_iterator.h: (__detail::__iter_key_t): Replace remove_const_t with __remove_cvref_t. (__detail::__iter_val_t): Apply __remove_cvref_t. * testsuite/23_containers/flat_map/1.cc: New tests. * testsuite/23_containers/flat_multimap/1.cc: New tests. * testsuite/23_containers/map/cons/deduction.cc: New tests. * testsuite/23_containers/map/cons/from_range.cc: New tests. * testsuite/23_containers/multimap/cons/deduction.cc: New tests. * testsuite/23_containers/multimap/cons/from_range.cc: New tests. * testsuite/23_containers/unordered_map/cons/deduction.cc: New tests. * testsuite/23_containers/unordered_map/cons/from_range.cc: New tests. * testsuite/23_containers/unordered_multimap/cons/deduction.cc: New tests. * testsuite/23_containers/unordered_multimap/cons/from_range.cc: New tests. Reviewed-by: Jonathan Wakely Signed-off-by: Tomasz Kamiński --- diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h index 488907da446..dde16498856 100644 --- a/libstdc++-v3/include/bits/ranges_base.h +++ b/libstdc++-v3/include/bits/ranges_base.h @@ -1103,11 +1103,11 @@ namespace __detail // 4223. Deduction guides for maps are mishandling tuples and references template using __range_key_type - = remove_const_t>>; + = remove_cvref_t>>; template using __range_mapped_type - = tuple_element_t<1, ranges::range_value_t<_Range>>; + = remove_cvref_t>>; // The allocator's value_type for map-like containers. template diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index 9203a66b2ff..bed72955d0c 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -3086,8 +3086,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cpp_deduction_guides >= 201606 // These helper traits are used for deduction guides // of associative containers. + + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 4223. Deduction guides for maps are mishandling tuples and references template - using __iter_key_t = remove_const_t< + using __iter_key_t = __remove_cvref_t< #ifdef __glibcxx_tuple_like // >= C++23 tuple_element_t<0, typename iterator_traits<_InputIterator>::value_type>>; #else @@ -3095,11 +3098,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif template - using __iter_val_t + using __iter_val_t = __remove_cvref_t< #ifdef __glibcxx_tuple_like // >= C++23 - = tuple_element_t<1, typename iterator_traits<_InputIterator>::value_type>; + tuple_element_t<1, typename iterator_traits<_InputIterator>::value_type>>; #else - = typename iterator_traits<_InputIterator>::value_type::second_type; + typename iterator_traits<_InputIterator>::value_type::second_type>; #endif template diff --git a/libstdc++-v3/testsuite/23_containers/flat_map/1.cc b/libstdc++-v3/testsuite/23_containers/flat_map/1.cc index 4fd33f616f7..a4e0d867c93 100644 --- a/libstdc++-v3/testsuite/23_containers/flat_map/1.cc +++ b/libstdc++-v3/testsuite/23_containers/flat_map/1.cc @@ -56,16 +56,17 @@ test_deduction_guide() std::vector>, std::vector>>>); - // LWG4223: deduces flat_map, which in turn instantiates - // std::vector that is ill-formed. - // __gnu_test::test_input_range> r2(0, 0); - // std::flat_map it5(r2.begin(), r2.begin()); - // std::flat_map fr5(std::from_range, r2); - - // LWG4223: deduces flat_map - //__gnu_test::test_input_range> r3(0, 0); - // std::flat_map it6(r3.begin(), r3.begin()); - // std::flat_map fr6(std::from_range, r3); + __gnu_test::test_input_range> r2(0, 0); + std::flat_map it5(r2.begin(), r2.begin()); + static_assert(std::is_same_v>); + std::flat_map fr5(std::from_range, r2); + static_assert(std::is_same_v>); + + __gnu_test::test_input_range> r3(0, 0); + std::flat_map it6(r3.begin(), r3.begin()); + static_assert(std::is_same_v>); + std::flat_map fr6(std::from_range, r3); + static_assert(std::is_same_v>); __gnu_test::test_input_range> r4(0, 0); std::flat_map it7(r4.begin(), r4.begin()); diff --git a/libstdc++-v3/testsuite/23_containers/flat_multimap/1.cc b/libstdc++-v3/testsuite/23_containers/flat_multimap/1.cc index ea0d4b41e9f..1e70535286a 100644 --- a/libstdc++-v3/testsuite/23_containers/flat_multimap/1.cc +++ b/libstdc++-v3/testsuite/23_containers/flat_multimap/1.cc @@ -54,16 +54,17 @@ test_deduction_guide() std::vector>, std::vector>>>); - // LWG4223: deduces flat_multimap, which in turn instantiates - // std::vector that is ill-formed. - // __gnu_test::test_input_range> r2(0, 0); - // std::flat_multimap it5(r2.begin(), r2.begin()); - // std::flat_multimap fr5(std::from_range, r2); - - // LWG4223: deduces flat_multimap - //__gnu_test::test_input_range> r3(0, 0); - // std::flat_multimap it6(r3.begin(), r3.begin()); - // std::flat_multimap fr6(std::from_range, r3); + __gnu_test::test_input_range> r2(0, 0); + std::flat_multimap it5(r2.begin(), r2.begin()); + static_assert(std::is_same_v>); + std::flat_multimap fr5(std::from_range, r2); + static_assert(std::is_same_v>); + + __gnu_test::test_input_range> r3(0, 0); + std::flat_multimap it6(r3.begin(), r3.begin()); + static_assert(std::is_same_v>); + std::flat_multimap fr6(std::from_range, r3); + static_assert(std::is_same_v>); __gnu_test::test_input_range> r4(0, 0); std::flat_multimap it7(r4.begin(), r4.begin()); diff --git a/libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc index f8e6e6e8b99..864598879b0 100644 --- a/libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc +++ b/libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc @@ -258,3 +258,49 @@ test_p1518r2() std::map s2(std::move(m), p); check_type(s2); } + +struct MyPred +{ + template + bool operator()(T const&, U const&) const; +}; + +template +constexpr bool test_lwg4223() +{ + using KD = std::remove_cv_t>; + using VD = std::remove_cv_t>; + using Alloc = __gnu_test::SimpleAllocator>; + + std::initializer_list> il = {}; + Alloc a; + MyPred p; + + // The remove_cvref_t is not applied here. + // static_assert(std::is_same_v< + // decltype(std::map(il)), + // std::map>); + + static_assert(std::is_same_v< + decltype(std::map(il.begin(), il.end())), + std::map>); + + static_assert(std::is_same_v< + decltype(std::map(il.begin(), il.end(), p)), + std::map>); + + static_assert(std::is_same_v< + decltype(std::map(il.begin(), il.end(), a)), + std::map, Alloc>>); + + static_assert(std::is_same_v< + decltype(std::map(il.begin(), il.end(), p, a)), + std::map>); + + return true; +} + +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); diff --git a/libstdc++-v3/testsuite/23_containers/map/cons/from_range.cc b/libstdc++-v3/testsuite/23_containers/map/cons/from_range.cc index 9935f44aa0f..3a9fede0434 100644 --- a/libstdc++-v3/testsuite/23_containers/map/cons/from_range.cc +++ b/libstdc++-v3/testsuite/23_containers/map/cons/from_range.cc @@ -43,11 +43,11 @@ test_deduction_guide() __gnu_test::test_input_range> r2(0, 0); std::map m5(std::from_range, r2); - static_assert(std::is_same_v>); + static_assert(std::is_same_v>); - // LWG4223: deduces map - //__gnu_test::test_input_range> r3(0, 0); - // std::map m6(std::from_range, r3); + __gnu_test::test_input_range> r3(0, 0); + std::map m6(std::from_range, r3); + static_assert(std::is_same_v>); __gnu_test::test_input_range> r4(0, 0); std::map m7(std::from_range, r4); diff --git a/libstdc++-v3/testsuite/23_containers/multimap/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/multimap/cons/deduction.cc index f0699e2eefc..de5e6acb4f2 100644 --- a/libstdc++-v3/testsuite/23_containers/multimap/cons/deduction.cc +++ b/libstdc++-v3/testsuite/23_containers/multimap/cons/deduction.cc @@ -210,3 +210,49 @@ test_p1518r2() std::multimap s2(std::move(m), p); check_type(s2); } + +struct MyPred +{ + template + bool operator()(T const&, U const&) const; +}; + +template +constexpr bool test_lwg4223() +{ + using KD = std::remove_cv_t>; + using VD = std::remove_cv_t>; + using Alloc = __gnu_test::SimpleAllocator>; + + std::initializer_list> il = {}; + Alloc a; + MyPred p; + + // The remove_cvref_t is not applied here. + // static_assert(std::is_same_v< + // decltype(std::multimap(il)), + // std::multimap>); + + static_assert(std::is_same_v< + decltype(std::multimap(il.begin(), il.end())), + std::multimap>); + + static_assert(std::is_same_v< + decltype(std::multimap(il.begin(), il.end(), p)), + std::multimap>); + + static_assert(std::is_same_v< + decltype(std::multimap(il.begin(), il.end(), a)), + std::multimap, Alloc>>); + + static_assert(std::is_same_v< + decltype(std::multimap(il.begin(), il.end(), p, a)), + std::multimap>); + + return true; +} + +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); diff --git a/libstdc++-v3/testsuite/23_containers/multimap/cons/from_range.cc b/libstdc++-v3/testsuite/23_containers/multimap/cons/from_range.cc index 4a8ea9fec7d..5907bab9878 100644 --- a/libstdc++-v3/testsuite/23_containers/multimap/cons/from_range.cc +++ b/libstdc++-v3/testsuite/23_containers/multimap/cons/from_range.cc @@ -43,11 +43,11 @@ test_deduction_guide() __gnu_test::test_input_range> r2(0, 0); std::multimap m5(std::from_range, r2); - static_assert(std::is_same_v>); + static_assert(std::is_same_v>); - // LWG4223: deduces multimap - //__gnu_test::test_input_range> r3(0, 0); - // std::multimap m6(std::from_range, r3); + __gnu_test::test_input_range> r3(0, 0); + std::multimap m6(std::from_range, r3); + static_assert(std::is_same_v>); __gnu_test::test_input_range> r4(0, 0); std::multimap m7(std::from_range, r4); diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/deduction.cc index fa182f5866d..26013da15c2 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/deduction.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/deduction.cc @@ -190,3 +190,72 @@ test_p1518r2() std::unordered_map s2(std::move(m), p); check_type(s2); } + +struct MyHash +{ + template + std::size_t operator()(T const&) const; +}; + +struct MyPred +{ + template + bool operator()(T const&, U const&) const; +}; + +template +constexpr bool test_lwg4223() +{ + using KD = std::remove_cv_t>; + using VD = std::remove_cv_t>; + using Alloc = __gnu_test::SimpleAllocator>; + + std::initializer_list> il = {}; + Alloc a; + MyHash h; + MyPred p; + + // The remove_cvref_t is not applied here. + // static_assert(std::is_same_v< + // decltype(std::unordered_map(il)), + // std::unordered_map>); + + static_assert(std::is_same_v< + decltype(std::unordered_map(il.begin(), il.end())), + std::unordered_map>); + + static_assert(std::is_same_v< + decltype(std::unordered_map(il.begin(), il.end(), 0)), + std::unordered_map>); + + static_assert(std::is_same_v< + decltype(std::unordered_map(il.begin(), il.end(), 0, h)), + std::unordered_map>); + + static_assert(std::is_same_v< + decltype(std::unordered_map(il.begin(), il.end(), 0, h, p)), + std::unordered_map>); + + static_assert(std::is_same_v< + decltype(std::unordered_map(il.begin(), il.end(), a)), + std::unordered_map, std::equal_to, Alloc>>); + + static_assert(std::is_same_v< + decltype(std::unordered_map(il.begin(), il.end(), 0, a)), + std::unordered_map, std::equal_to, Alloc>>); + + static_assert(std::is_same_v< + decltype(std::unordered_map(il.begin(), il.end(), 0, h, a)), + std::unordered_map, Alloc>>); + + static_assert(std::is_same_v< + decltype(std::unordered_map(il.begin(), il.end(), 0, h, p, a)), + std::unordered_map>); + + return true; +} + +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/from_range.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/from_range.cc index 36efc2de5a3..04479a5e54d 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/from_range.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/from_range.cc @@ -78,13 +78,11 @@ test_deduction_guide() __gnu_test::test_input_range> r2(0, 0); std::unordered_map m9(std::from_range, r2); - static_assert(std::is_same_v< - decltype(m9), - std::unordered_map>); + static_assert(std::is_same_v>); - // LWG4223: deduces map - // __gnu_test::test_input_range> r3(0, 0); - // std::unordered_map m10(std::from_range, r3); + __gnu_test::test_input_range> r3(0, 0); + std::unordered_map m10(std::from_range, r3); + static_assert(std::is_same_v>); __gnu_test::test_input_range> r4(0, 0); std::unordered_map m11(std::from_range, r4); diff --git a/libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/deduction.cc index 4de23fe3e79..1db58a04abf 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/deduction.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/deduction.cc @@ -205,3 +205,72 @@ test_p1518r2() std::unordered_multimap s2(std::move(m), p); check_type(s2); } + +struct MyHash +{ + template + std::size_t operator()(T const&) const; +}; + +struct MyPred +{ + template + bool operator()(T const&, U const&) const; +}; + +template +constexpr bool test_lwg4223() +{ + using KD = std::remove_cv_t>; + using VD = std::remove_cv_t>; + using Alloc = __gnu_test::SimpleAllocator>; + + std::initializer_list> il = {}; + Alloc a; + MyHash h; + MyPred p; + + // The remove_cvref_t is not applied here. + // static_assert(std::is_same_v< + // decltype(std::unordered_multimap(il)), + // std::unordered_multimap>); + + static_assert(std::is_same_v< + decltype(std::unordered_multimap(il.begin(), il.end())), + std::unordered_multimap>); + + static_assert(std::is_same_v< + decltype(std::unordered_multimap(il.begin(), il.end(), 0)), + std::unordered_multimap>); + + static_assert(std::is_same_v< + decltype(std::unordered_multimap(il.begin(), il.end(), 0, h)), + std::unordered_multimap>); + + static_assert(std::is_same_v< + decltype(std::unordered_multimap(il.begin(), il.end(), 0, h, p)), + std::unordered_multimap>); + + static_assert(std::is_same_v< + decltype(std::unordered_multimap(il.begin(), il.end(), a)), + std::unordered_multimap, std::equal_to, Alloc>>); + + static_assert(std::is_same_v< + decltype(std::unordered_multimap(il.begin(), il.end(), 0, a)), + std::unordered_multimap, std::equal_to, Alloc>>); + + static_assert(std::is_same_v< + decltype(std::unordered_multimap(il.begin(), il.end(), 0, h, a)), + std::unordered_multimap, Alloc>>); + + static_assert(std::is_same_v< + decltype(std::unordered_multimap(il.begin(), il.end(), 0, h, p, a)), + std::unordered_multimap>); + + return true; +} + +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); +static_assert(test_lwg4223()); diff --git a/libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/from_range.cc b/libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/from_range.cc index b551df49f3e..4567bd8aa91 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/from_range.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/from_range.cc @@ -86,11 +86,13 @@ test_deduction_guide() std::unordered_multimap m9(std::from_range, r2); static_assert(std::is_same_v< decltype(m9), - std::unordered_multimap>); + std::unordered_multimap>); - // LWG4223: deduces map - // __gnu_test::test_input_range> r3(0, 0); - // std::unordered_multimap m10(std::from_range, r3); + __gnu_test::test_input_range> r3(0, 0); + std::unordered_multimap m10(std::from_range, r3); + static_assert(std::is_same_v< + decltype(m10), + std::unordered_multimap>); __gnu_test::test_input_range> r4(0, 0); std::unordered_multimap m11(std::from_range, r4);