|
| 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 | +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 9 | + |
| 10 | +#include <array> |
| 11 | +#include <flat_map> |
| 12 | +#include <iostream> |
| 13 | +#include <map> |
| 14 | +#include <set> |
| 15 | +#include <string> |
| 16 | +#include <tuple> |
| 17 | +#include <type_traits> |
| 18 | +#include <unordered_map> |
| 19 | +#include <utility> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include "test_macros.h" |
| 23 | + |
| 24 | +int main(int, char**) { |
| 25 | + // --- Input Data --- |
| 26 | + // 1. Vector of std::pair |
| 27 | + std::vector<std::pair<const int, std::string>> pair_vec = {{1, "apple"}, {2, "banana"}, {3, "cherry"}}; |
| 28 | + |
| 29 | + // 2. Vector of std::tuple |
| 30 | + std::vector<std::tuple<int, double>> tuple_vec = {{10, 1.1}, {20, 2.2}, {30, 3.3}}; |
| 31 | + |
| 32 | + // 3. Vector of std::array |
| 33 | + std::vector<std::array<long, 2>> array_vec = {{100L, 101L}, {200L, 201L}, {300L, 301L}}; |
| 34 | + |
| 35 | + // 4. Vector of std::pair with non-const key (for testing const addition in iter_to_alloc_type) |
| 36 | + std::vector<std::pair<int, std::string>> non_const_key_pair_vec = {{5, "grape"}, {6, "kiwi"}}; |
| 37 | + |
| 38 | + // --- CTAD Tests --- |
| 39 | + |
| 40 | + // map |
| 41 | + std::map m1(pair_vec.begin(), pair_vec.end()); |
| 42 | + static_assert(std::is_same_v<decltype(m1), std::map<int, std::string>>); |
| 43 | + |
| 44 | + // multimap |
| 45 | + std::multimap mm1(pair_vec.begin(), pair_vec.end()); |
| 46 | + static_assert(std::is_same_v<decltype(mm1), std::multimap<int, std::string>>); |
| 47 | + |
| 48 | + // unordered_map |
| 49 | + std::unordered_map um1(pair_vec.begin(), pair_vec.end()); |
| 50 | + static_assert(std::is_same_v<decltype(um1), std::unordered_map<int, std::string>>); |
| 51 | + |
| 52 | + // unordered_multimap |
| 53 | + std::unordered_multimap umm1(pair_vec.begin(), pair_vec.end()); |
| 54 | + static_assert(std::is_same_v<decltype(umm1), std::unordered_multimap<int, std::string>>); |
| 55 | + |
| 56 | + // flat_map |
| 57 | + std::flat_map fm1(pair_vec.begin(), pair_vec.end()); |
| 58 | + static_assert(std::is_same_v<decltype(fm1), std::flat_map<int, std::string>>); |
| 59 | + |
| 60 | + // flat_multimap |
| 61 | + std::flat_multimap fmm1(pair_vec.begin(), pair_vec.end()); |
| 62 | + static_assert(std::is_same_v<decltype(fmm1), std::flat_multimap<int, std::string>>); |
| 63 | + |
| 64 | + // map |
| 65 | + std::map m2(tuple_vec.begin(), tuple_vec.end()); |
| 66 | + static_assert(std::is_same_v<decltype(m2), std::map<int, double>>); |
| 67 | + |
| 68 | + // multimap |
| 69 | + std::multimap mm2(tuple_vec.begin(), tuple_vec.end()); |
| 70 | + static_assert(std::is_same_v<decltype(mm2), std::multimap<int, double>>); |
| 71 | + |
| 72 | + // unordered_map |
| 73 | + std::unordered_map um2(tuple_vec.begin(), tuple_vec.end()); |
| 74 | + // Note: std::tuple needs a hash specialization to be used as a key in unordered containers. |
| 75 | + // CTAD itself should work, but compilation/runtime might fail without a hash. |
| 76 | + // This static_assert checks the deduced type. A hash specialization would be needed for actual use. |
| 77 | + static_assert(std::is_same_v<decltype(um2), std::unordered_map<int, double>>); |
| 78 | + |
| 79 | + // unordered_multimap |
| 80 | + std::unordered_multimap umm2(tuple_vec.begin(), tuple_vec.end()); |
| 81 | + static_assert(std::is_same_v<decltype(umm2), std::unordered_multimap<int, double>>); |
| 82 | + |
| 83 | + // flat_map |
| 84 | + std::flat_map fm2(tuple_vec.begin(), tuple_vec.end()); |
| 85 | + static_assert(std::is_same_v<decltype(fm2), std::flat_map<int, double>>); |
| 86 | + |
| 87 | + // flat_multimap |
| 88 | + std::flat_multimap fmm2(tuple_vec.begin(), tuple_vec.end()); |
| 89 | + static_assert(std::is_same_v<decltype(fmm2), std::flat_multimap<int, double>>); |
| 90 | + |
| 91 | + // map |
| 92 | + std::map m3(array_vec.begin(), array_vec.end()); |
| 93 | + static_assert(std::is_same_v<decltype(m3), std::map<long, long>>); |
| 94 | + |
| 95 | + // multimap |
| 96 | + std::multimap mm3(array_vec.begin(), array_vec.end()); |
| 97 | + static_assert(std::is_same_v<decltype(mm3), std::multimap<long, long>>); |
| 98 | + |
| 99 | + // unordered_map |
| 100 | + std::unordered_map um3(array_vec.begin(), array_vec.end()); |
| 101 | + // Note: std::array needs a hash specialization. |
| 102 | + static_assert(std::is_same_v<decltype(um3), std::unordered_map<long, long>>); |
| 103 | + |
| 104 | + // unordered_multimap |
| 105 | + std::unordered_multimap umm3(array_vec.begin(), array_vec.end()); |
| 106 | + static_assert(std::is_same_v<decltype(umm3), std::unordered_multimap<long, long>>); |
| 107 | + |
| 108 | + // flat_map |
| 109 | + std::flat_map fm3(array_vec.begin(), array_vec.end()); |
| 110 | + static_assert(std::is_same_v<decltype(fm3), std::flat_map<long, long>>); |
| 111 | + |
| 112 | + // flat_multimap |
| 113 | + std::flat_multimap fmm3(array_vec.begin(), array_vec.end()); |
| 114 | + static_assert(std::is_same_v<decltype(fmm3), std::flat_multimap<long, long>>); |
| 115 | + |
| 116 | + // map |
| 117 | + std::map m4(non_const_key_pair_vec.begin(), non_const_key_pair_vec.end()); |
| 118 | + static_assert(std::is_same_v<decltype(m4), std::map<int, std::string>>); |
| 119 | + |
| 120 | + return 0; |
| 121 | +} |
0 commit comments