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