| 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 <array> |
| 30 | #include <climits> // INT_MAX |
| 31 | #include <functional> |
| 32 | #include <map> |
| 33 | #include <tuple> |
| 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 | // cannot deduce Key and T from nothing |
| 46 | std::map m; |
| 47 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 48 | } |
| 49 | { |
| 50 | // cannot deduce Key and T from just (Compare) |
| 51 | std::map m(std::less<int>{}); |
| 52 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}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-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 58 | } |
| 59 | { |
| 60 | // cannot deduce Key and T from just (Allocator) |
| 61 | std::map m(std::allocator<PC>{}); |
| 62 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}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 assertion 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); |
| 74 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 75 | } |
| 76 | { |
| 77 | // cannot deduce that the inner braced things should be std::pair and not something else |
| 78 | std::map m{{1, 1L}, {2, 2L}, {3, 3L}}; |
| 79 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 80 | } |
| 81 | { |
| 82 | // cannot deduce that the inner braced things should be std::pair and not something else |
| 83 | std::map m({{1, 1L}, {2, 2L}, {3, 3L}}, std::less<int>()); |
| 84 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 85 | } |
| 86 | { |
| 87 | // cannot deduce that the inner braced things should be std::pair and not something else |
| 88 | std::map m({{1, 1L}, {2, 2L}, {3, 3L}}, std::less<int>(), std::allocator<PC>()); |
| 89 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 90 | } |
| 91 | { |
| 92 | // cannot deduce that the inner braced things should be std::pair and not something else |
| 93 | std::map m({{1, 1L}, {2, 2L}, {3, 3L}}, std::allocator<PC>()); |
| 94 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 95 | } |
| 96 | { |
| 97 | // since we have parens, not braces, this deliberately does not find the initializer_list constructor |
| 98 | std::map m(P{1, 1L}); |
| 99 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 100 | } |
| 101 | { |
| 102 | // since we have parens, not braces, this deliberately does not find the initializer_list constructor |
| 103 | std::map m(PC{1, 1L}); |
| 104 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 105 | } |
| 106 | { |
| 107 | // cannot deduce from tuple-like objects without proper iterator |
| 108 | std::tuple<int, double> t{1, 2.0}; |
| 109 | std::map m(t); |
| 110 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 111 | } |
| 112 | { |
| 113 | // cannot deduce from array-like objects without proper iterator |
| 114 | std::array<int, 2> arr{1, 2}; |
| 115 | std::map m(arr); |
| 116 | // expected-error-re@-1{{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}map'}} |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |