[libcxx-commits] [libcxx] [libc++] P2165R4: Update deduction guides for map containers and container adaptors (PR #136011)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Apr 23 03:04:59 PDT 2025


================
@@ -0,0 +1,123 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+#include <array>
+#include <flat_map>
+#include <iostream>
+#include <map>
+#include <set> 
+#include <string>
+#include <tuple>
+#include <type_traits>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+#include "test_macros.h"
+
+int main(int, char**) {
+    #if TEST_STD_VER >= 23
+    // --- Input Data ---
+    // 1. Vector of std::pair
+    std::vector<std::pair<const int, std::string>> pair_vec = {{1, "apple"}, {2, "banana"}, {3, "cherry"}};
+
+    // 2. Vector of std::tuple
+    std::vector<std::tuple<int, double>> tuple_vec = {{10, 1.1}, {20, 2.2}, {30, 3.3}};
+
+    // 3. Vector of std::array
+    std::vector<std::array<long, 2>> array_vec = {{100L, 101L}, {200L, 201L}, {300L, 301L}};
+
+    // 4. Vector of std::pair with non-const key (for testing const addition in iter_to_alloc_type)
+    std::vector<std::pair<int, std::string>> non_const_key_pair_vec = {{5, "grape"}, {6, "kiwi"}};
+
+
+    // --- CTAD Tests ---
+
+    // map
+    std::map m1(pair_vec.begin(), pair_vec.end());
+    static_assert(std::is_same_v<decltype(m1), std::map<int, std::string>>);
----------------
frederick-vs-ja wrote:

> I initially tried to find an existing test file where these deduction guide checks (covering `map`, `unordered_map`, `flat_map`, etc.) would fit naturally.

Ah, no, the test suite tests CTAD for different containers/container adaptors in separated files.

IIUC we can add new test cases to these files:
- `libcxx/test/std/containers/associative/map/map.cons/deduct.pass.cpp`
- `libcxx/test/std/containers/associative/multimap/multimap.cons/deduct.pass.cpp`
- `libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.pass.cpp`
- `libcxx/test/std/containers/container.adaptors/flat.multimap/flat.multimap.cons/deduct.pass.cpp`
- `libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/deduct.pass.cpp`
- `libcxx/test/std/containers/unord/unord.multimap/unord.map.multimap/deduct.pass.cpp`

And verify that the some cases didn't work until C++23 in adjacent `*.deduct.verify.cpp`.

https://github.com/llvm/llvm-project/pull/136011


More information about the libcxx-commits mailing list