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, c++17, c++20
10
11// <flat_map>
12
13// explicit flat_map(const key_compare& comp);
14// template <class Alloc>
15// flat_map(const key_compare& comp, const Alloc& a);
16
17#include <deque>
18#include <flat_map>
19#include <functional>
20#include <type_traits>
21#include <vector>
22
23#include "test_macros.h"
24#include "../../../test_compare.h"
25#include "test_allocator.h"
26
27int main(int, char**) {
28 {
29 // The constructors in this subclause shall not participate in overload
30 // resolution unless uses_allocator_v<key_container_type, Alloc> is true
31 // and uses_allocator_v<mapped_container_type, Alloc> is true.
32
33 using C = test_less<int>;
34 using A1 = test_allocator<int>;
35 using A2 = other_allocator<int>;
36 using M1 = std::flat_map<int, int, C, std::vector<int, A1>, std::vector<int, A1>>;
37 using M2 = std::flat_map<int, int, C, std::vector<int, A1>, std::vector<int, A2>>;
38 using M3 = std::flat_map<int, int, C, std::vector<int, A2>, std::vector<int, A1>>;
39 static_assert(std::is_constructible_v<M1, const C&, const A1&>);
40 static_assert(!std::is_constructible_v<M1, const C&, const A2&>);
41 static_assert(!std::is_constructible_v<M2, const C&, const A2&>);
42 static_assert(!std::is_constructible_v<M3, const C&, const A2&>);
43 }
44 {
45 using C = test_less<int>;
46 auto m = std::flat_map<int, char*, C>(C(3));
47 assert(m.empty());
48 assert(m.begin() == m.end());
49 assert(m.key_comp() == C(3));
50 }
51 {
52 // The one-argument ctor is explicit.
53 using C = test_less<int>;
54 static_assert(std::is_constructible_v<std::flat_map<int, char*, C>, C>);
55 static_assert(!std::is_convertible_v<C, std::flat_map<int, char*, C>>);
56
57 static_assert(std::is_constructible_v<std::flat_map<int, char*>, std::less<int>>);
58 static_assert(!std::is_convertible_v<std::less<int>, std::flat_map<int, char*>>);
59 }
60 {
61 using C = test_less<int>;
62 using A1 = test_allocator<int>;
63 using A2 = test_allocator<short>;
64 auto m = std::flat_map<int, short, C, std::vector<int, A1>, std::vector<short, A2>>(C(4), A1(5));
65 assert(m.empty());
66 assert(m.begin() == m.end());
67 assert(m.key_comp() == C(4));
68 assert(m.keys().get_allocator() == A1(5));
69 assert(m.values().get_allocator() == A2(5));
70 }
71 {
72 // explicit(false)
73 using C = test_less<int>;
74 using A1 = test_allocator<int>;
75 using A2 = test_allocator<short>;
76 std::flat_map<int, short, C, std::deque<int, A1>, std::deque<short, A2>> m = {C(4), A1(5)};
77 assert(m.empty());
78 assert(m.begin() == m.end());
79 assert(m.key_comp() == C(4));
80 assert(m.keys().get_allocator() == A1(5));
81 assert(m.values().get_allocator() == A2(5));
82 }
83 {
84 // If an allocator is given, it must be usable by both containers.
85 using A = test_allocator<int>;
86 using M = std::flat_map<int, int, std::less<>, std::vector<int>, std::vector<int, A>>;
87 static_assert(std::is_constructible_v<M, std::less<>>);
88 static_assert(!std::is_constructible_v<M, std::less<>, std::allocator<int>>);
89 static_assert(!std::is_constructible_v<M, std::less<>, A>);
90 }
91
92 return 0;
93}
94

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/compare.pass.cpp