[libcxx-commits] [libcxx] 598983d - [libc++][P2321R2] Add specializations of basic_common_reference and common_type for pair

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 2 01:49:15 PST 2022


Author: Nikolas Klauser
Date: 2022-02-02T10:47:04+01:00
New Revision: 598983d7a96e19bb84200bed6d8c7e1968123dbe

URL: https://github.com/llvm/llvm-project/commit/598983d7a96e19bb84200bed6d8c7e1968123dbe
DIFF: https://github.com/llvm/llvm-project/commit/598983d7a96e19bb84200bed6d8c7e1968123dbe.diff

LOG: [libc++][P2321R2] Add specializations of basic_common_reference and common_type for pair

Add specializations of basic_common_reference and common_type for pair

Reviewed By: Quuxplusone, Mordante, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D117506

Added: 
    

Modified: 
    libcxx/docs/Status/ZipProjects.csv
    libcxx/include/__utility/pair.h
    libcxx/include/utility
    libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_reference.compile.pass.cpp
    libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Status/ZipProjects.csv b/libcxx/docs/Status/ZipProjects.csv
index 4990d6b3e10f9..30fd859293e50 100644
--- a/libcxx/docs/Status/ZipProjects.csv
+++ b/libcxx/docs/Status/ZipProjects.csv
@@ -1,7 +1,7 @@
 Section,Description,Dependencies,Assignee,Complete
-| `[tuple.syn] <https://wg21.link/tuple.syn>`_, "`[tuple] basic_common_reference, common_type <https://reviews.llvm.org/D116538>`_", None, Nikolas Klauser, |In Progress|
+| `[tuple.syn] <https://wg21.link/tuple.syn>`_, "`[tuple] basic_common_reference, common_type <https://reviews.llvm.org/D116538>`_", None, Nikolas Klauser, |Complete|
 | `[tuple.tuple] <https://wg21.link/tuple.tuple>`_, "`[tuple] constructor, assignment and swap overloads <https://reviews.llvm.org/D116621>`_", None, Nikolas Klauser, |In Progress|
-| `[utility.syn] <https://wg21.link/utility.syn>`_, "[pair] basic_common_reference, common_type", None, Nikolas Klauser, |Not Started|
+| `[utility.syn] <https://wg21.link/utility.syn>`_, "[pair] basic_common_reference, common_type", None, Nikolas Klauser, |Complete|
 | `[pairs.pair] <https://wg21.link/pairs.pair>`_, "[pair] constructor, assignment and swap overloads", None, Nikolas Klauser, |Not Started|
 "| `[memory.syn] <https://wg21.link/memory.syn>`_
 | `[allocator.uses.construction] <https://wg21.link/allocator.uses.construction>`_", "[pair] uses_allocator_construction_args overloads", None, Unassigned, |Not Started|

diff  --git a/libcxx/include/__utility/pair.h b/libcxx/include/__utility/pair.h
index f1114696884ac..2c8593ac3b66c 100644
--- a/libcxx/include/__utility/pair.h
+++ b/libcxx/include/__utility/pair.h
@@ -315,7 +315,7 @@ struct _LIBCPP_TEMPLATE_VIS pair
 #endif
 };
 
-#if _LIBCPP_STD_VER >= 17
+#if _LIBCPP_STD_VER > 14
 template<class _T1, class _T2>
 pair(_T1, _T2) -> pair<_T1, _T2>;
 #endif
@@ -389,6 +389,22 @@ operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
 
 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
 
+#if _LIBCPP_STD_VER > 20
+template <class _T1, class _T2, class _U1, class _U2, template<class> class _TQual, template<class> class _UQual>
+    requires requires { typename pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
+                                      common_reference_t<_TQual<_T2>, _UQual<_U2>>>; }
+struct basic_common_reference<pair<_T1, _T2>, pair<_U1, _U2>, _TQual, _UQual> {
+    using type = pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
+                      common_reference_t<_TQual<_T2>, _UQual<_U2>>>;
+};
+
+template <class _T1, class _T2, class _U1, class _U2>
+    requires requires { typename pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>; }
+struct common_type<pair<_T1, _T2>, pair<_U1, _U2>> {
+    using type = pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>;
+};
+#endif
+
 template <class _T1, class _T2>
 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 typename enable_if

diff  --git a/libcxx/include/utility b/libcxx/include/utility
index 9dd7905516a86..db68181a67a91 100644
--- a/libcxx/include/utility
+++ b/libcxx/include/utility
@@ -95,6 +95,12 @@ struct pair
                                 is_nothrow_swappable_v<T2>);                     // constexpr in C++20
 };
 
+template<class T1, class T2, class U1, class U2, template<class> class TQual, template<class> class UQual>
+struct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual>;         // since C++23
+
+template<class T1, class T2, class U1, class U2>
+struct common_type<pair<T1, T2>, pair<U1, U2>>;                                  // since C++23
+
 template<class T1, class T2> pair(T1, T2) -> pair<T1, T2>;
 
 template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14

diff  --git a/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_reference.compile.pass.cpp b/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_reference.compile.pass.cpp
index 17421c17f1dee..135f933ebbcb9 100644
--- a/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_reference.compile.pass.cpp
+++ b/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_reference.compile.pass.cpp
@@ -15,6 +15,7 @@
 
 #include <tuple>
 #include <type_traits>
+#include <utility>
 
 #include "test_macros.h"
 
@@ -197,6 +198,25 @@ struct std::basic_common_reference<A, std::tuple<B>, TQual, UQual> {
 
 static_assert(std::is_same_v<std::common_reference_t<A, std::tuple<B>,std::tuple<D>>, std::tuple<B>>);
 
+
+static_assert(std::is_same_v<std::common_reference_t<std::pair<int, int>>,
+                             std::pair<int, int>>);
+static_assert(std::is_same_v<std::common_reference_t<std::pair<int, long>, std::pair<long, int>>,
+                             std::pair<long, long>>);
+static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, const int&>, std::pair<const int&, int>>,
+                             std::pair<const int&, int>>);
+static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, volatile int&>, std::pair<volatile int&, int>>,
+                             std::pair<volatile int&, int>>);
+static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, const volatile int&>, std::pair<const volatile int&, int>>,
+                             std::pair<const volatile int&, int>>);
+static_assert(!has_type<std::common_reference_t<std::pair<const int&, volatile int&>,
+                        std::pair<volatile int&, const int&>>>);
+
+static_assert(std::is_same_v<std::common_reference_t<std::pair<int, X2>, std::pair<int, Y2>>, std::pair<int, Z2>>);
+static_assert(std::is_same_v<std::common_reference_t<std::pair<int, X2>, std::pair<int, Y2>>, std::pair<int, Z2>>);
+static_assert(!has_type<std::common_reference<std::pair<int, const X2>, std::pair<float, const Z2>>>);
+static_assert(!has_type<std::common_reference<std::pair<int, X2>, std::pair<float, Z2>>>);
+static_assert(!has_type<std::common_reference<std::pair<int, X2>, int, X2>>);
 #endif
 
 int main(int, char**) { return 0; }

diff  --git a/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp b/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp
index 901443a2c4bd2..2842d9835d408 100644
--- a/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp
+++ b/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp
@@ -14,6 +14,7 @@
 #include <memory>
 #include <tuple>
 #include <type_traits>
+#include <utility>
 
 #include "test_macros.h"
 
@@ -234,11 +235,11 @@ void test_bullet_three_four() {
   static_assert(std::is_same_v<std::common_type_t<const bad_reference_wrapper<double>, double>, double>, "");
   static_assert(std::is_same_v<std::common_type_t<volatile bad_reference_wrapper<double>, double>, double>, "");
   static_assert(std::is_same_v<std::common_type_t<const volatile bad_reference_wrapper<double>, double>, double>, "");
-  
+
   static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>, const double>, double>, "");
   static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>, volatile double>, double>, "");
   static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>, const volatile double>, double>, "");
-  
+
   static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>&, double>, double>, "");
   static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>, double&>, double>, "");
 #endif
@@ -359,7 +360,20 @@ int main(int, char**)
     static_assert(std::is_same_v<std::common_type_t<std::tuple<const int&>>, std::tuple<int>>);
     static_assert(std::is_same_v<std::common_type_t<std::tuple<const volatile int&>, std::tuple<const volatile long&>>, std::tuple<long>>);
 
-    static_assert(std::is_same_v<std::common_reference_t<A, std::tuple<B>,std::tuple<C>>, std::tuple<B>>);
+    static_assert(std::is_same_v<std::common_type_t<A, std::tuple<B>,std::tuple<C>>, std::tuple<B>>);
+
+    static_assert(std::is_same_v<std::common_type_t<std::pair<int, int>>, std::pair<int, int>>);
+    static_assert(std::is_same_v<std::common_type_t<std::pair<int, long>, std::pair<long, int>>, std::pair<long, long>>);
+    static_assert(std::is_same_v<std::common_type_t<std::pair<const int, const long>,
+                                                    std::pair<const int, const long>>,
+                                                    std::pair<int, long>>);
+
+    static_assert(std::is_same_v<std::common_type_t<std::pair<const int&, const long&>>, std::pair<int, long>>);
+    static_assert(std::is_same_v<std::common_type_t<std::pair<const volatile int&, const volatile long&>,
+                                                    std::pair<const volatile long&, const volatile int&>>,
+                                                    std::pair<long, long>>);
+
+    static_assert(std::is_same_v<std::common_type_t<A, std::tuple<B>,std::tuple<C>>, std::tuple<B>>);
 #endif
 
   return 0;


        


More information about the libcxx-commits mailing list