Skip to content

Commit d6c3e29

Browse files
committed
[libc++] P2165R4: Update deduction guides for map containers and container adaptors
1 parent 51fa6cd commit d6c3e29

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

libcxx/include/__iterator/iterator_traits.h

+14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <__fwd/pair.h>
2323
#include <__iterator/incrementable_traits.h>
2424
#include <__iterator/readable_traits.h>
25+
#include <__tuple/tuple_element.h>
2526
#include <__type_traits/common_reference.h>
2627
#include <__type_traits/conditional.h>
2728
#include <__type_traits/detected_or.h>
@@ -466,6 +467,18 @@ using __has_exactly_bidirectional_iterator_category _LIBCPP_NODEBUG =
466467
template <class _InputIterator>
467468
using __iter_value_type _LIBCPP_NODEBUG = typename iterator_traits<_InputIterator>::value_type;
468469

470+
#if _LIBCPP_STD_VER >= 23
471+
template <class _InputIterator>
472+
using __iter_key_type _LIBCPP_NODEBUG = __remove_const_t<tuple_element_t<0, __iter_value_type<_InputIterator>>>;
473+
474+
template <class _InputIterator>
475+
using __iter_mapped_type _LIBCPP_NODEBUG = tuple_element_t<1, __iter_value_type<_InputIterator>>;
476+
477+
template <class _InputIterator>
478+
using __iter_to_alloc_type _LIBCPP_NODEBUG =
479+
pair<const tuple_element_t<0, __iter_value_type<_InputIterator>>,
480+
tuple_element_t<1, __iter_value_type<_InputIterator>>>;
481+
#else
469482
template <class _InputIterator>
470483
using __iter_key_type _LIBCPP_NODEBUG =
471484
__remove_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>;
@@ -477,6 +490,7 @@ template <class _InputIterator>
477490
using __iter_to_alloc_type _LIBCPP_NODEBUG =
478491
pair<const typename iterator_traits<_InputIterator>::value_type::first_type,
479492
typename iterator_traits<_InputIterator>::value_type::second_type>;
493+
#endif // _LIBCPP_STD_VER >= 23
480494

481495
template <class _Iter>
482496
using __iterator_category_type _LIBCPP_NODEBUG = typename iterator_traits<_Iter>::iterator_category;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9+
10+
#include <array>
11+
#include <flat_map>
12+
#include <iostream>
13+
#include <map>
14+
#include <set>
15+
#include <string>
16+
#include <tuple>
17+
#include <type_traits>
18+
#include <unordered_map>
19+
#include <utility>
20+
#include <vector>
21+
22+
#include "test_macros.h"
23+
24+
int main(int, char**) {
25+
// --- Input Data ---
26+
// 1. Vector of std::pair
27+
std::vector<std::pair<const int, std::string>> pair_vec = {{1, "apple"}, {2, "banana"}, {3, "cherry"}};
28+
29+
// 2. Vector of std::tuple
30+
std::vector<std::tuple<int, double>> tuple_vec = {{10, 1.1}, {20, 2.2}, {30, 3.3}};
31+
32+
// 3. Vector of std::array
33+
std::vector<std::array<long, 2>> array_vec = {{100L, 101L}, {200L, 201L}, {300L, 301L}};
34+
35+
// 4. Vector of std::pair with non-const key (for testing const addition in iter_to_alloc_type)
36+
std::vector<std::pair<int, std::string>> non_const_key_pair_vec = {{5, "grape"}, {6, "kiwi"}};
37+
38+
// --- CTAD Tests ---
39+
40+
// map
41+
std::map m1(pair_vec.begin(), pair_vec.end());
42+
static_assert(std::is_same_v<decltype(m1), std::map<int, std::string>>);
43+
44+
// multimap
45+
std::multimap mm1(pair_vec.begin(), pair_vec.end());
46+
static_assert(std::is_same_v<decltype(mm1), std::multimap<int, std::string>>);
47+
48+
// unordered_map
49+
std::unordered_map um1(pair_vec.begin(), pair_vec.end());
50+
static_assert(std::is_same_v<decltype(um1), std::unordered_map<int, std::string>>);
51+
52+
// unordered_multimap
53+
std::unordered_multimap umm1(pair_vec.begin(), pair_vec.end());
54+
static_assert(std::is_same_v<decltype(umm1), std::unordered_multimap<int, std::string>>);
55+
56+
// flat_map
57+
std::flat_map fm1(pair_vec.begin(), pair_vec.end());
58+
static_assert(std::is_same_v<decltype(fm1), std::flat_map<int, std::string>>);
59+
60+
// flat_multimap
61+
std::flat_multimap fmm1(pair_vec.begin(), pair_vec.end());
62+
static_assert(std::is_same_v<decltype(fmm1), std::flat_multimap<int, std::string>>);
63+
64+
// map
65+
std::map m2(tuple_vec.begin(), tuple_vec.end());
66+
static_assert(std::is_same_v<decltype(m2), std::map<int, double>>);
67+
68+
// multimap
69+
std::multimap mm2(tuple_vec.begin(), tuple_vec.end());
70+
static_assert(std::is_same_v<decltype(mm2), std::multimap<int, double>>);
71+
72+
// unordered_map
73+
std::unordered_map um2(tuple_vec.begin(), tuple_vec.end());
74+
// Note: std::tuple needs a hash specialization to be used as a key in unordered containers.
75+
// CTAD itself should work, but compilation/runtime might fail without a hash.
76+
// This static_assert checks the deduced type. A hash specialization would be needed for actual use.
77+
static_assert(std::is_same_v<decltype(um2), std::unordered_map<int, double>>);
78+
79+
// unordered_multimap
80+
std::unordered_multimap umm2(tuple_vec.begin(), tuple_vec.end());
81+
static_assert(std::is_same_v<decltype(umm2), std::unordered_multimap<int, double>>);
82+
83+
// flat_map
84+
std::flat_map fm2(tuple_vec.begin(), tuple_vec.end());
85+
static_assert(std::is_same_v<decltype(fm2), std::flat_map<int, double>>);
86+
87+
// flat_multimap
88+
std::flat_multimap fmm2(tuple_vec.begin(), tuple_vec.end());
89+
static_assert(std::is_same_v<decltype(fmm2), std::flat_multimap<int, double>>);
90+
91+
// map
92+
std::map m3(array_vec.begin(), array_vec.end());
93+
static_assert(std::is_same_v<decltype(m3), std::map<long, long>>);
94+
95+
// multimap
96+
std::multimap mm3(array_vec.begin(), array_vec.end());
97+
static_assert(std::is_same_v<decltype(mm3), std::multimap<long, long>>);
98+
99+
// unordered_map
100+
std::unordered_map um3(array_vec.begin(), array_vec.end());
101+
// Note: std::array needs a hash specialization.
102+
static_assert(std::is_same_v<decltype(um3), std::unordered_map<long, long>>);
103+
104+
// unordered_multimap
105+
std::unordered_multimap umm3(array_vec.begin(), array_vec.end());
106+
static_assert(std::is_same_v<decltype(umm3), std::unordered_multimap<long, long>>);
107+
108+
// flat_map
109+
std::flat_map fm3(array_vec.begin(), array_vec.end());
110+
static_assert(std::is_same_v<decltype(fm3), std::flat_map<long, long>>);
111+
112+
// flat_multimap
113+
std::flat_multimap fmm3(array_vec.begin(), array_vec.end());
114+
static_assert(std::is_same_v<decltype(fmm3), std::flat_multimap<long, long>>);
115+
116+
// map
117+
std::map m4(non_const_key_pair_vec.begin(), non_const_key_pair_vec.end());
118+
static_assert(std::is_same_v<decltype(m4), std::map<int, std::string>>);
119+
120+
return 0;
121+
}

0 commit comments

Comments
 (0)