Skip to content

Commit e20865c

Browse files
committed
[libc++] Implement deduction guides for <set>
This is part of C++17's P0433. Thanks to Arthur O'Dwyer for the patch. Differential Revision: https://reviews.llvm.org/D58582 llvm-svn: 363090
1 parent 9970817 commit e20865c

File tree

5 files changed

+574
-2
lines changed

5 files changed

+574
-2
lines changed

libcxx/include/set

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public:
446446
typedef key_type value_type;
447447
typedef _Compare key_compare;
448448
typedef key_compare value_compare;
449-
typedef _Allocator allocator_type;
449+
typedef typename __identity<_Allocator>::type allocator_type;
450450
typedef value_type& reference;
451451
typedef const value_type& const_reference;
452452

@@ -840,6 +840,34 @@ public:
840840
#endif
841841
};
842842

843+
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
844+
template<class _InputIterator,
845+
class _Compare = less<typename iterator_traits<_InputIterator>::value_type>,
846+
class _Allocator = allocator<typename iterator_traits<_InputIterator>::value_type>,
847+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type,
848+
class = typename enable_if<!__is_allocator<_Compare>::value, void>::type>
849+
set(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
850+
-> set<typename iterator_traits<_InputIterator>::value_type, _Compare, _Allocator>;
851+
852+
template<class _Key, class _Compare = less<_Key>,
853+
class _Allocator = allocator<_Key>,
854+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type,
855+
class = typename enable_if<!__is_allocator<_Compare>::value, void>::type>
856+
set(initializer_list<_Key>, _Compare = _Compare(), _Allocator = _Allocator())
857+
-> set<_Key, _Compare, _Allocator>;
858+
859+
template<class _InputIterator, class _Allocator,
860+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type>
861+
set(_InputIterator, _InputIterator, _Allocator)
862+
-> set<typename iterator_traits<_InputIterator>::value_type,
863+
less<typename iterator_traits<_InputIterator>::value_type>, _Allocator>;
864+
865+
template<class _Key, class _Allocator,
866+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type>
867+
set(initializer_list<_Key>, _Allocator)
868+
-> set<_Key, less<_Key>, _Allocator>;
869+
#endif
870+
843871
#ifndef _LIBCPP_CXX03_LANG
844872

845873
template <class _Key, class _Compare, class _Allocator>
@@ -938,7 +966,7 @@ public:
938966
typedef key_type value_type;
939967
typedef _Compare key_compare;
940968
typedef key_compare value_compare;
941-
typedef _Allocator allocator_type;
969+
typedef typename __identity<_Allocator>::type allocator_type;
942970
typedef value_type& reference;
943971
typedef const value_type& const_reference;
944972

@@ -1332,6 +1360,34 @@ public:
13321360
#endif
13331361
};
13341362

1363+
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1364+
template<class _InputIterator,
1365+
class _Compare = less<typename iterator_traits<_InputIterator>::value_type>,
1366+
class _Allocator = allocator<typename iterator_traits<_InputIterator>::value_type>,
1367+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type,
1368+
class = typename enable_if<!__is_allocator<_Compare>::value, void>::type>
1369+
multiset(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
1370+
-> multiset<typename iterator_traits<_InputIterator>::value_type, _Compare, _Allocator>;
1371+
1372+
template<class _Key, class _Compare = less<_Key>,
1373+
class _Allocator = allocator<_Key>,
1374+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type,
1375+
class = typename enable_if<!__is_allocator<_Compare>::value, void>::type>
1376+
multiset(initializer_list<_Key>, _Compare = _Compare(), _Allocator = _Allocator())
1377+
-> multiset<_Key, _Compare, _Allocator>;
1378+
1379+
template<class _InputIterator, class _Allocator,
1380+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type>
1381+
multiset(_InputIterator, _InputIterator, _Allocator)
1382+
-> multiset<typename iterator_traits<_InputIterator>::value_type,
1383+
less<typename iterator_traits<_InputIterator>::value_type>, _Allocator>;
1384+
1385+
template<class _Key, class _Allocator,
1386+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type>
1387+
multiset(initializer_list<_Key>, _Allocator)
1388+
-> multiset<_Key, less<_Key>, _Allocator>;
1389+
#endif
1390+
13351391
#ifndef _LIBCPP_CXX03_LANG
13361392

13371393
template <class _Key, class _Compare, class _Allocator>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// <set>
10+
// UNSUPPORTED: c++98, c++03, c++11, c++14
11+
// UNSUPPORTED: libcpp-no-deduction-guides
12+
13+
// template<class InputIterator,
14+
// class Compare = less<iter-value-type<InputIterator>>,
15+
// class Allocator = allocator<iter-value-type<InputIterator>>>
16+
// multiset(InputIterator, InputIterator,
17+
// Compare = Compare(), Allocator = Allocator())
18+
// -> multiset<iter-value-type<InputIterator>, Compare, Allocator>;
19+
// template<class Key, class Compare = less<Key>,
20+
// class Allocator = allocator<Key>>
21+
// multiset(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
22+
// -> multiset<Key, Compare, Allocator>;
23+
// template<class InputIterator, class Allocator>
24+
// multiset(InputIterator, InputIterator, Allocator)
25+
// -> multiset<iter-value-type<InputIterator>,
26+
// less<iter-value-type<InputIterator>>, Allocator>;
27+
// template<class Key, class Allocator>
28+
// multiset(initializer_list<Key>, Allocator)
29+
// -> multiset<Key, less<Key>, Allocator>;
30+
31+
#include <functional>
32+
#include <set>
33+
#include <type_traits>
34+
35+
struct NotAnAllocator {
36+
friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
37+
};
38+
39+
int main(int, char **) {
40+
{
41+
// cannot deduce Key from nothing
42+
std::multiset s;
43+
// expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'multiset'}}
44+
}
45+
{
46+
// cannot deduce Key from just (Compare)
47+
std::multiset s(std::less<int>{});
48+
// expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'multiset'}}
49+
}
50+
{
51+
// cannot deduce Key from just (Compare, Allocator)
52+
std::multiset s(std::less<int>{}, std::allocator<int>{});
53+
// expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'multiset'}}
54+
}
55+
{
56+
// cannot deduce Key from multiset(Allocator)
57+
std::multiset s(std::allocator<int>{});
58+
// expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'multiset'}}
59+
}
60+
{
61+
// since we have parens, not braces, this deliberately does not find the
62+
// initializer_list constructor
63+
NotAnAllocator a;
64+
std::multiset s(a);
65+
// expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'multiset'}}
66+
}
67+
68+
return 0;
69+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
// <set>
10+
// UNSUPPORTED: c++98, c++03, c++11, c++14
11+
// UNSUPPORTED: libcpp-no-deduction-guides
12+
13+
// template<class InputIterator,
14+
// class Compare = less<iter-value-type<InputIterator>>,
15+
// class Allocator = allocator<iter-value-type<InputIterator>>>
16+
// multiset(InputIterator, InputIterator,
17+
// Compare = Compare(), Allocator = Allocator())
18+
// -> multiset<iter-value-type<InputIterator>, Compare, Allocator>;
19+
// template<class Key, class Compare = less<Key>,
20+
// class Allocator = allocator<Key>>
21+
// multiset(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
22+
// -> multiset<Key, Compare, Allocator>;
23+
// template<class InputIterator, class Allocator>
24+
// multiset(InputIterator, InputIterator, Allocator)
25+
// -> multiset<iter-value-type<InputIterator>,
26+
// less<iter-value-type<InputIterator>>, Allocator>;
27+
// template<class Key, class Allocator>
28+
// multiset(initializer_list<Key>, Allocator)
29+
// -> multiset<Key, less<Key>, Allocator>;
30+
31+
#include <algorithm> // std::equal
32+
#include <cassert>
33+
#include <climits> // INT_MAX
34+
#include <functional>
35+
#include <set>
36+
#include <type_traits>
37+
38+
#include "test_allocator.h"
39+
40+
struct NotAnAllocator {
41+
friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
42+
};
43+
44+
int main(int, char **) {
45+
{
46+
const int arr[] = { 1, 2, 1, INT_MAX, 3 };
47+
std::multiset s(std::begin(arr), std::end(arr));
48+
49+
ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
50+
const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
51+
assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
52+
std::end(expected_s)));
53+
}
54+
55+
{
56+
const int arr[] = { 1, 2, 1, INT_MAX, 3 };
57+
std::multiset s(std::begin(arr), std::end(arr), std::greater<int>());
58+
59+
ASSERT_SAME_TYPE(decltype(s), std::multiset<int, std::greater<int> >);
60+
const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
61+
assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
62+
std::end(expected_s)));
63+
}
64+
65+
{
66+
const int arr[] = { 1, 2, 1, INT_MAX, 3 };
67+
std::multiset s(std::begin(arr), std::end(arr), std::greater<int>(),
68+
test_allocator<int>(0, 42));
69+
70+
ASSERT_SAME_TYPE(
71+
decltype(s),
72+
std::multiset<int, std::greater<int>, test_allocator<int> >);
73+
const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
74+
assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
75+
std::end(expected_s)));
76+
assert(s.get_allocator().get_id() == 42);
77+
}
78+
79+
{
80+
std::multiset<long> source;
81+
std::multiset s(source);
82+
ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
83+
assert(s.size() == 0);
84+
}
85+
86+
{
87+
std::multiset<long> source;
88+
std::multiset s{ source }; // braces instead of parens
89+
ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
90+
assert(s.size() == 0);
91+
}
92+
93+
{
94+
std::multiset<long> source;
95+
std::multiset s(source, std::multiset<long>::allocator_type());
96+
ASSERT_SAME_TYPE(decltype(s), std::multiset<long>);
97+
assert(s.size() == 0);
98+
}
99+
100+
{
101+
std::multiset s{ 1, 2, 1, INT_MAX, 3 };
102+
103+
ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
104+
const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
105+
assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
106+
std::end(expected_s)));
107+
}
108+
109+
{
110+
std::multiset s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>());
111+
112+
ASSERT_SAME_TYPE(decltype(s), std::multiset<int, std::greater<int> >);
113+
const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
114+
assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
115+
std::end(expected_s)));
116+
}
117+
118+
{
119+
std::multiset s({ 1, 2, 1, INT_MAX, 3 }, std::greater<int>(),
120+
test_allocator<int>(0, 43));
121+
122+
ASSERT_SAME_TYPE(
123+
decltype(s),
124+
std::multiset<int, std::greater<int>, test_allocator<int> >);
125+
const int expected_s[] = { INT_MAX, 3, 2, 1, 1 };
126+
assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
127+
std::end(expected_s)));
128+
assert(s.get_allocator().get_id() == 43);
129+
}
130+
131+
{
132+
const int arr[] = { 1, 2, 1, INT_MAX, 3 };
133+
std::multiset s(std::begin(arr), std::end(arr), test_allocator<int>(0, 44));
134+
135+
ASSERT_SAME_TYPE(decltype(s),
136+
std::multiset<int, std::less<int>, test_allocator<int> >);
137+
const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
138+
assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
139+
std::end(expected_s)));
140+
assert(s.get_allocator().get_id() == 44);
141+
}
142+
143+
{
144+
std::multiset s({ 1, 2, 1, INT_MAX, 3 }, test_allocator<int>(0, 45));
145+
146+
ASSERT_SAME_TYPE(decltype(s),
147+
std::multiset<int, std::less<int>, test_allocator<int> >);
148+
const int expected_s[] = { 1, 1, 2, 3, INT_MAX };
149+
assert(std::equal(s.begin(), s.end(), std::begin(expected_s),
150+
std::end(expected_s)));
151+
assert(s.get_allocator().get_id() == 45);
152+
}
153+
154+
{
155+
NotAnAllocator a;
156+
std::multiset s{ a }; // multiset(initializer_list<NotAnAllocator>)
157+
ASSERT_SAME_TYPE(decltype(s), std::multiset<NotAnAllocator>);
158+
assert(s.size() == 1);
159+
}
160+
161+
{
162+
std::multiset<long> source;
163+
std::multiset s{ source, source }; // multiset(initializer_list<multiset<long>>)
164+
ASSERT_SAME_TYPE(decltype(s), std::multiset<std::multiset<long> >);
165+
assert(s.size() == 2);
166+
}
167+
168+
{
169+
NotAnAllocator a;
170+
std::multiset s{ a, a }; // multiset(initializer_list<NotAnAllocator>)
171+
ASSERT_SAME_TYPE(decltype(s), std::multiset<NotAnAllocator>);
172+
assert(s.size() == 2);
173+
}
174+
175+
{
176+
int source[3] = { 3, 4, 5 };
177+
std::multiset s(source, source + 3); // multiset(InputIterator, InputIterator)
178+
ASSERT_SAME_TYPE(decltype(s), std::multiset<int>);
179+
assert(s.size() == 3);
180+
}
181+
182+
{
183+
int source[3] = { 3, 4, 5 };
184+
std::multiset s{ source, source + 3 }; // multiset(initializer_list<int*>)
185+
ASSERT_SAME_TYPE(decltype(s), std::multiset<int *>);
186+
assert(s.size() == 2);
187+
}
188+
189+
return 0;
190+
}

0 commit comments

Comments
 (0)