Skip to content

[libc++] P2165R4: Update deduction guides for map containers and container adaptors #136011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

KSARK
Copy link

@KSARK KSARK commented Apr 16, 2025

Fixes #135351

This PR update CATD guides to associative containers (std::map, std::multimap, std::unordered_map, std::unordered_multimap, std::flat_map, std::flat_multimap).

  • Updated template alias for deduction guides for the relevant associative containers.
  • Added a new test to verify the deduction guides with std::map, std::multimap, std::unordered_map, std::unordered_multimap, std::flat_map, std::flat_multimap.

@KSARK KSARK requested a review from a team as a code owner April 16, 2025 19:11
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2025

@llvm/pr-subscribers-libcxx

Author: Ksar (KSARK)

Changes

Fixes #135351

This PR update CATD guides to associative containers (std::map, std::multimap, std::unordered_map, std::unordered_multimap, std::flat_map, std::flat_multimap).

  • Updated template alias for deduction guides for the relevant associative containers.
  • Added a new test to verify the deduction guides with std::map, std::multimap, std::unordered_map, std::unordered_multimap, std::flat_map, std::flat_multimap.

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

2 Files Affected:

  • (modified) libcxx/include/__iterator/iterator_traits.h (+14)
  • (added) libcxx/test/std/containers/associative/associative.general/P2165R4_deduction_guides.pass.cpp (+121)
diff --git a/libcxx/include/__iterator/iterator_traits.h b/libcxx/include/__iterator/iterator_traits.h
index c08d72971ec3e..d72e1faef3670 100644
--- a/libcxx/include/__iterator/iterator_traits.h
+++ b/libcxx/include/__iterator/iterator_traits.h
@@ -22,6 +22,7 @@
 #include <__fwd/pair.h>
 #include <__iterator/incrementable_traits.h>
 #include <__iterator/readable_traits.h>
+#include <__tuple/tuple_element.h>
 #include <__type_traits/common_reference.h>
 #include <__type_traits/conditional.h>
 #include <__type_traits/detected_or.h>
@@ -466,6 +467,19 @@ using __has_exactly_bidirectional_iterator_category _LIBCPP_NODEBUG =
 template <class _InputIterator>
 using __iter_value_type _LIBCPP_NODEBUG = typename iterator_traits<_InputIterator>::value_type;
 
+#if _LIBCPP_STD_VER >= 23
+template <class _InputIterator>
+using __iter_key_type _LIBCPP_NODEBUG = 
+    __remove_const_t<tuple_element_t<0, __iter_value_type<_InputIterator>>>;
+
+template <class _InputIterator>
+using __iter_mapped_type _LIBCPP_NODEBUG = tuple_element_t<1, __iter_value_type<_InputIterator>>;
+
+template <class _InputIterator>
+using __iter_to_alloc_type _LIBCPP_NODEBUG =
+    pair<const tuple_element_t<0, __iter_value_type<_InputIterator>>,
+        tuple_element_t<1, __iter_value_type<_InputIterator>>>;
+#else
 template <class _InputIterator>
 using __iter_key_type _LIBCPP_NODEBUG =
     __remove_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>;
diff --git a/libcxx/test/std/containers/associative/associative.general/P2165R4_deduction_guides.pass.cpp b/libcxx/test/std/containers/associative/associative.general/P2165R4_deduction_guides.pass.cpp
new file mode 100644
index 0000000000000..d45c43fd220ac
--- /dev/null
+++ b/libcxx/test/std/containers/associative/associative.general/P2165R4_deduction_guides.pass.cpp
@@ -0,0 +1,121 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+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>>);
+
+    // multimap
+    std::multimap mm1(pair_vec.begin(), pair_vec.end());
+    static_assert(std::is_same_v<decltype(mm1), std::multimap<int, std::string>>);
+
+    // unordered_map
+    std::unordered_map um1(pair_vec.begin(), pair_vec.end());
+    static_assert(std::is_same_v<decltype(um1), std::unordered_map<int, std::string>>);
+    
+    // unordered_multimap
+    std::unordered_multimap umm1(pair_vec.begin(), pair_vec.end());
+    static_assert(std::is_same_v<decltype(umm1), std::unordered_multimap<int, std::string>>);
+
+    // flat_map
+    std::flat_map fm1(pair_vec.begin(), pair_vec.end());
+    static_assert(std::is_same_v<decltype(fm1), std::flat_map<int, std::string>>);
+
+    // flat_multimap
+    std::flat_multimap fmm1(pair_vec.begin(), pair_vec.end());
+    static_assert(std::is_same_v<decltype(fmm1), std::flat_multimap<int, std::string>>);
+
+    // map
+    std::map m2(tuple_vec.begin(), tuple_vec.end());
+    static_assert(std::is_same_v<decltype(m2), std::map<int, double>>);
+
+    // multimap
+    std::multimap mm2(tuple_vec.begin(), tuple_vec.end());
+    static_assert(std::is_same_v<decltype(mm2), std::multimap<int, double>>);
+
+    // unordered_map
+    std::unordered_map um2(tuple_vec.begin(), tuple_vec.end());
+    // Note: std::tuple needs a hash specialization to be used as a key in unordered containers.
+    // CTAD itself should work, but compilation/runtime might fail without a hash.
+    // This static_assert checks the deduced type. A hash specialization would be needed for actual use.
+    static_assert(std::is_same_v<decltype(um2), std::unordered_map<int, double>>);
+
+    // unordered_multimap
+    std::unordered_multimap umm2(tuple_vec.begin(), tuple_vec.end());
+    static_assert(std::is_same_v<decltype(umm2), std::unordered_multimap<int, double>>);
+
+    // flat_map
+    std::flat_map fm2(tuple_vec.begin(), tuple_vec.end());
+    static_assert(std::is_same_v<decltype(fm2), std::flat_map<int, double>>);
+
+    // flat_multimap
+    std::flat_multimap fmm2(tuple_vec.begin(), tuple_vec.end());
+    static_assert(std::is_same_v<decltype(fmm2), std::flat_multimap<int, double>>);
+
+    // map
+    std::map m3(array_vec.begin(), array_vec.end());
+    static_assert(std::is_same_v<decltype(m3), std::map<long, long>>);
+
+    // multimap
+    std::multimap mm3(array_vec.begin(), array_vec.end());
+    static_assert(std::is_same_v<decltype(mm3), std::multimap<long, long>>);
+
+    // unordered_map
+    std::unordered_map um3(array_vec.begin(), array_vec.end());
+    // Note: std::array needs a hash specialization.
+    static_assert(std::is_same_v<decltype(um3), std::unordered_map<long, long>>);
+
+    // unordered_multimap
+    std::unordered_multimap umm3(array_vec.begin(), array_vec.end());
+    static_assert(std::is_same_v<decltype(umm3), std::unordered_multimap<long, long>>);
+
+     // flat_map
+    std::flat_map fm3(array_vec.begin(), array_vec.end());
+    static_assert(std::is_same_v<decltype(fm3), std::flat_map<long, long>>);
+
+    // flat_multimap
+    std::flat_multimap fmm3(array_vec.begin(), array_vec.end());
+    static_assert(std::is_same_v<decltype(fmm3), std::flat_multimap<long, long>>);
+
+    // map
+    std::map m4(non_const_key_pair_vec.begin(), non_const_key_pair_vec.end());
+    static_assert(std::is_same_v<decltype(m4), std::map<int, std::string>>);
+    #endif
+    return 0;
+}


// map
std::map m1(pair_vec.begin(), pair_vec.end());
static_assert(std::is_same_v<decltype(m1), std::map<int, std::string>>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether it would be better to add the new test cases to existing test files and check the behavior changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, is it better to add behind a iterator test or separately insert to certain containers' test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried to find an existing test file where these deduction guide checks (covering map, unordered_map, flat_map, etc.) would fit naturally. Since none seemed quite right for such broad associative container tests, creating a dedicated file in associative.general seemed like the cleanest solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

github-actions bot commented Apr 17, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc++] P2165R4: Update deduction guides for map containers and container adaptors
4 participants