llvm-project
105 строк · 4.9 Кб
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// map(InputIterator, InputIterator,
17// Compare = Compare(), Allocator = Allocator())
18// -> map<iter-value-type<InputIterator>, Compare, Allocator>;
19// template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>>
20// map(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
21// -> map<Key, Compare, Allocator>;
22// template<class InputIterator, class Allocator>
23// map(InputIterator, InputIterator, Allocator)
24// -> map<iter-value-type<InputIterator>, less<iter-value-type<InputIterator>>, Allocator>;
25// template<class Key, class Allocator>
26// map(initializer_list<Key>, Allocator)
27// -> map<Key, less<Key>, Allocator>;
28
29#include <climits> // INT_MAX
30#include <functional>
31#include <map>
32#include <type_traits>
33
34struct NotAnAllocator {
35friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
36};
37
38using P = std::pair<int, long>;
39using PC = std::pair<const int, long>;
40
41int main(int, char**)
42{
43{
44// cannot deduce Key and T from nothing
45std::map m; // expected-error-re {{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}}
46}
47{
48// cannot deduce Key and T from just (Compare)
49std::map m(std::less<int>{});
50// expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}}
51}
52{
53// cannot deduce Key and T from just (Compare, Allocator)
54std::map 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::)?}}map'}}
56}
57{
58// cannot deduce Key and T from just (Allocator)
59std::map m(std::allocator<PC>{});
60// expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}}
61}
62{
63// refuse to rebind the allocator if Allocator::value_type is not exactly what we expect
64const P arr[] = { {1,1L}, {2,2L}, {3,3L} };
65std::map 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
70NotAnAllocator a;
71std::map m(a); // expected-error-re{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}}
72}
73{
74// cannot deduce that the inner braced things should be std::pair and not something else
75std::map 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::)?}}map'}}
77}
78{
79// cannot deduce that the inner braced things should be std::pair and not something else
80std::map 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::)?}}map'}}
82}
83{
84// cannot deduce that the inner braced things should be std::pair and not something else
85std::map 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::)?}}map'}}
87}
88{
89// cannot deduce that the inner braced things should be std::pair and not something else
90std::map 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::)?}}map'}}
92}
93{
94// since we have parens, not braces, this deliberately does not find the initializer_list constructor
95std::map m(P{1,1L});
96// expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}}
97}
98{
99// since we have parens, not braces, this deliberately does not find the initializer_list constructor
100std::map m(PC{1,1L});
101// expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}}
102}
103
104return 0;
105}
106