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 // UNSUPPORTED: libcpp-no-deduction-guides
12 // XFAIL: clang-6, apple-clang-9.0, apple-clang-9.1, apple-clang-10.0.0
13 //  clang-6 gives different error messages.
14 
15 // template<class InputIterator,
16 //          class Compare = less<iter-value-type<InputIterator>>,
17 //          class Allocator = allocator<iter-value-type<InputIterator>>>
18 // map(InputIterator, InputIterator,
19 //          Compare = Compare(), Allocator = Allocator())
20 //   -> map<iter-value-type<InputIterator>, Compare, Allocator>;
21 // template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>>
22 // map(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
23 //   -> map<Key, Compare, Allocator>;
24 // template<class InputIterator, class Allocator>
25 // map(InputIterator, InputIterator, Allocator)
26 //   -> map<iter-value-type<InputIterator>, less<iter-value-type<InputIterator>>, Allocator>;
27 // template<class Key, class Allocator>
28 // map(initializer_list<Key>, Allocator)
29 //   -> map<Key, less<Key>, Allocator>;
30 
31 #include <climits> // INT_MAX
32 #include <functional>
33 #include <map>
34 #include <type_traits>
35 
36 struct NotAnAllocator {
37     friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
38 };
39 
40 using P = std::pair<int, long>;
41 using PC = std::pair<const int, long>;
42 
43 int main(int, char**)
44 {
45     {
46         // cannot deduce Key and T from nothing
47         std::map m; // expected-error{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
48     }
49     {
50         // cannot deduce Key and T from just (Compare)
51         std::map m(std::less<int>{});
52             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
53     }
54     {
55         // cannot deduce Key and T from just (Compare, Allocator)
56         std::map m(std::less<int>{}, std::allocator<PC>{});
57             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
58     }
59     {
60         // cannot deduce Key and T from just (Allocator)
61         std::map m(std::allocator<PC>{});
62             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
63     }
64     {
65         // refuse to rebind the allocator if Allocator::value_type is not exactly what we expect
66         const P arr[] = { {1,1L}, {2,2L}, {3,3L} };
67         std::map m(arr, arr + 3, std::allocator<P>());
68             // expected-error-re@map:* {{static_assert failed{{( due to requirement '.*')?}} "Allocator::value_type must be same type as value_type"}}
69     }
70     {
71         // cannot convert from some arbitrary unrelated type
72         NotAnAllocator a;
73         std::map m(a); // expected-error{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
74     }
75     {
76         // cannot deduce that the inner braced things should be std::pair and not something else
77         std::map m{ {1,1L}, {2,2L}, {3,3L} };
78             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
79     }
80     {
81         // cannot deduce that the inner braced things should be std::pair and not something else
82         std::map m({ {1,1L}, {2,2L}, {3,3L} }, std::less<int>());
83             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
84     }
85     {
86         // cannot deduce that the inner braced things should be std::pair and not something else
87         std::map m({ {1,1L}, {2,2L}, {3,3L} }, std::less<int>(), std::allocator<PC>());
88             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
89     }
90     {
91         // cannot deduce that the inner braced things should be std::pair and not something else
92         std::map m({ {1,1L}, {2,2L}, {3,3L} }, std::allocator<PC>());
93             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
94     }
95     {
96         // since we have parens, not braces, this deliberately does not find the initializer_list constructor
97         std::map m(P{1,1L});
98             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
99     }
100     {
101         // since we have parens, not braces, this deliberately does not find the initializer_list constructor
102         std::map m(PC{1,1L});
103             // expected-error@-1{{no viable constructor or deduction guide for deduction of template arguments of 'map'}}
104     }
105 
106     return 0;
107 }
108