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
9// <map>
10// UNSUPPORTED: c++03, c++11, c++14
11
12// template<class InputIterator,
13// class Compare = less<iter-value-type<InputIterator>>,
14// class Allocator = allocator<iter-value-type<InputIterator>>>
15// multimap(InputIterator, InputIterator,
16// Compare = Compare(), Allocator = Allocator())
17// -> multimap<iter-value-type<InputIterator>, Compare, Allocator>;
18// template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>>
19// multimap(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
20// -> multimap<Key, Compare, Allocator>;
21// template<class InputIterator, class Allocator>
22// multimap(InputIterator, InputIterator, Allocator)
23// -> multimap<iter-value-type<InputIterator>, less<iter-value-type<InputIterator>>, Allocator>;
24// template<class Key, class Allocator>
25// multimap(initializer_list<Key>, Allocator)
26// -> multimap<Key, less<Key>, Allocator>;
27//
28// template<ranges::input_range R, class Compare = less<range-key-type<R>>,
29// class Allocator = allocator<range-to-alloc-type<R>>>
30// multimap(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
31// -> multimap<range-key-type<R>, range-mapped-type<R>, Compare, Allocator>; // C++23
32//
33// template<ranges::input_range R, class Allocator>
34// multimap(from_range_t, R&&, Allocator)
35// -> multimap<range-key-type<R>, range-mapped-type<R>, less<range-key-type<R>>, Allocator>; // C++23
36
37#include <algorithm> // std::equal
38#include <array>
39#include <cassert>
40#include <climits> // INT_MAX
41#include <functional>
42#include <map>
43#include <tuple>
44#include <type_traits>
45#include <utility>
46#include <vector>
47
48#include "deduction_guides_sfinae_checks.h"
49#include "test_allocator.h"
50
51using P = std::pair<int, long>;
52using PC = std::pair<const int, long>;
53
54int main(int, char**) {
55 {
56 const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
57 std::multimap m(std::begin(arr: arr), std::end(arr: arr));
58
59 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long>);
60 const PC expected_m[] = {{1, 1L}, {1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};
61 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
62 }
63
64 {
65 const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
66 std::multimap m(std::begin(arr: arr), std::end(arr: arr), std::greater<int>());
67
68 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::greater<int>>);
69 const PC expected_m[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}, {1, 1L}};
70 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
71 }
72
73 {
74 const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
75 std::multimap m(std::begin(arr: arr), std::end(arr: arr), std::greater<int>(), test_allocator<PC>(0, 42));
76
77 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::greater<int>, test_allocator<PC>>);
78 const PC expected_m[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}, {1, 1L}};
79 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
80 assert(m.get_allocator().get_id() == 42);
81 }
82
83 {
84 std::multimap<int, long> source;
85 std::multimap m(source);
86 ASSERT_SAME_TYPE(decltype(m), decltype(source));
87 assert(m.size() == 0);
88 }
89
90 {
91 std::multimap<int, long> source;
92 std::multimap m{source}; // braces instead of parens
93 ASSERT_SAME_TYPE(decltype(m), decltype(source));
94 assert(m.size() == 0);
95 }
96
97 {
98 std::multimap<int, long> source;
99 std::multimap m(source, std::map<int, long>::allocator_type());
100 ASSERT_SAME_TYPE(decltype(m), decltype(source));
101 assert(m.size() == 0);
102 }
103
104 {
105 std::multimap m{P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}};
106
107 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long>);
108 const PC expected_m[] = {{1, 1L}, {1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};
109 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
110 }
111
112 {
113 std::multimap m({P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, std::greater<int>());
114
115 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::greater<int>>);
116 const PC expected_m[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}, {1, 1L}};
117 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
118 }
119
120 {
121 std::multimap m(
122 {P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, std::greater<int>(), test_allocator<PC>(0, 43));
123
124 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::greater<int>, test_allocator<PC>>);
125 const PC expected_m[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}, {1, 1L}};
126 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
127 assert(m.get_allocator().get_id() == 43);
128 }
129
130 {
131 const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};
132 std::multimap m(std::begin(arr: arr), std::end(arr: arr), test_allocator<PC>(0, 44));
133
134 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::less<int>, test_allocator<PC>>);
135 const PC expected_m[] = {{1, 1L}, {1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};
136 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
137 assert(m.get_allocator().get_id() == 44);
138 }
139
140 {
141 std::multimap m({P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, test_allocator<PC>(0, 45));
142
143 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::less<int>, test_allocator<PC>>);
144 const PC expected_m[] = {{1, 1L}, {1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};
145 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
146 assert(m.get_allocator().get_id() == 45);
147 }
148
149 {
150 // Examples from LWG3025
151 std::multimap m{std::pair{1, 1}, {2, 2}, {3, 3}};
152 ASSERT_SAME_TYPE(decltype(m), std::multimap<int, int>);
153
154 std::multimap m2{m.begin(), m.end()};
155 ASSERT_SAME_TYPE(decltype(m2), std::multimap<int, int>);
156 }
157
158 {
159 // Examples from LWG3531
160 std::multimap m1{{std::pair{1, 2}, {3, 4}}, std::less<int>()};
161 ASSERT_SAME_TYPE(decltype(m1), std::multimap<int, int>);
162
163 using value_type = std::pair<const int, int>;
164 std::multimap m2{{value_type{1, 2}, {3, 4}}, std::less<int>()};
165 ASSERT_SAME_TYPE(decltype(m2), std::multimap<int, int>);
166 }
167
168#if TEST_STD_VER >= 23
169 {
170 using Range = std::array<P, 0>;
171 using Comp = std::greater<int>;
172 using DefaultComp = std::less<int>;
173 using Alloc = test_allocator<PC>;
174
175 { // (from_range, range)
176 std::multimap c(std::from_range, Range());
177 static_assert(std::is_same_v<decltype(c), std::multimap<int, long>>);
178 }
179
180 { // (from_range, range, comp)
181 std::multimap c(std::from_range, Range(), Comp());
182 static_assert(std::is_same_v<decltype(c), std::multimap<int, long, Comp>>);
183 }
184
185 { // (from_range, range, comp, alloc)
186 std::multimap c(std::from_range, Range(), Comp(), Alloc());
187 static_assert(std::is_same_v<decltype(c), std::multimap<int, long, Comp, Alloc>>);
188 }
189
190 { // (from_range, range, alloc)
191 std::multimap c(std::from_range, Range(), Alloc());
192 static_assert(std::is_same_v<decltype(c), std::multimap<int, long, DefaultComp, Alloc>>);
193 }
194 }
195 {
196 std::vector<std::pair<const int, float>> pair_vec = {{1, 1.1f}, {2, 2.2f}, {3, 3.3f}};
197 std::multimap mm1(pair_vec.begin(), pair_vec.end());
198 ASSERT_SAME_TYPE(decltype(mm1), std::multimap<int, float>);
199
200 std::vector<std::tuple<int, double>> tuple_vec = {{10, 1.1}, {20, 2.2}, {30, 3.3}};
201 std::multimap mm2(tuple_vec.begin(), tuple_vec.end());
202 ASSERT_SAME_TYPE(decltype(mm2), std::multimap<int, double>);
203
204 std::vector<std::array<long, 2>> array_vec = {{100L, 101L}, {200L, 201L}, {300L, 301L}};
205 std::multimap mm3(array_vec.begin(), array_vec.end());
206 ASSERT_SAME_TYPE(decltype(mm3), std::multimap<long, long>);
207
208 // Check deduction with non-const key in input pair
209 std::vector<std::pair<int, char>> non_const_key_pair_vec = {{5, 'a'}, {6, 'b'}};
210 std::multimap mm4(non_const_key_pair_vec.begin(), non_const_key_pair_vec.end());
211 ASSERT_SAME_TYPE(decltype(mm4), std::multimap<int, char>);
212 }
213#endif
214
215 AssociativeContainerDeductionGuidesSfinaeAway<std::multimap, std::multimap<int, long>>();
216
217 return 0;
218}
219

source code of libcxx/test/std/containers/associative/multimap/multimap.cons/deduct.pass.cpp