Skip to content

Commit c51a37c

Browse files
philnik777memfrob
authored andcommitted
[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
1 parent 4423c6a commit c51a37c

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

libcxx/docs/Status/ZipProjects.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Section,Description,Dependencies,Assignee,Complete
2-
| `[tuple.syn] <https://wg21.link/tuple.syn>`_, "`[tuple] basic_common_reference, common_type <https://reviews.llvm.org/D116538>`_", None, Nikolas Klauser, |In Progress|
2+
| `[tuple.syn] <https://wg21.link/tuple.syn>`_, "`[tuple] basic_common_reference, common_type <https://reviews.llvm.org/D116538>`_", None, Nikolas Klauser, |Complete|
33
| `[tuple.tuple] <https://wg21.link/tuple.tuple>`_, "`[tuple] constructor, assignment and swap overloads <https://reviews.llvm.org/D116621>`_", None, Nikolas Klauser, |In Progress|
4-
| `[utility.syn] <https://wg21.link/utility.syn>`_, "[pair] basic_common_reference, common_type", None, Nikolas Klauser, |Not Started|
4+
| `[utility.syn] <https://wg21.link/utility.syn>`_, "[pair] basic_common_reference, common_type", None, Nikolas Klauser, |Complete|
55
| `[pairs.pair] <https://wg21.link/pairs.pair>`_, "[pair] constructor, assignment and swap overloads", None, Nikolas Klauser, |Not Started|
66
"| `[memory.syn] <https://wg21.link/memory.syn>`_
77
| `[allocator.uses.construction] <https://wg21.link/allocator.uses.construction>`_", "[pair] uses_allocator_construction_args overloads", None, Unassigned, |Not Started|

libcxx/include/__utility/pair.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ struct _LIBCPP_TEMPLATE_VIS pair
315315
#endif
316316
};
317317

318-
#if _LIBCPP_STD_VER >= 17
318+
#if _LIBCPP_STD_VER > 14
319319
template<class _T1, class _T2>
320320
pair(_T1, _T2) -> pair<_T1, _T2>;
321321
#endif
@@ -389,6 +389,22 @@ operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
389389

390390
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
391391

392+
#if _LIBCPP_STD_VER > 20
393+
template <class _T1, class _T2, class _U1, class _U2, template<class> class _TQual, template<class> class _UQual>
394+
requires requires { typename pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
395+
common_reference_t<_TQual<_T2>, _UQual<_U2>>>; }
396+
struct basic_common_reference<pair<_T1, _T2>, pair<_U1, _U2>, _TQual, _UQual> {
397+
using type = pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
398+
common_reference_t<_TQual<_T2>, _UQual<_U2>>>;
399+
};
400+
401+
template <class _T1, class _T2, class _U1, class _U2>
402+
requires requires { typename pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>; }
403+
struct common_type<pair<_T1, _T2>, pair<_U1, _U2>> {
404+
using type = pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>;
405+
};
406+
#endif
407+
392408
template <class _T1, class _T2>
393409
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
394410
typename enable_if

libcxx/include/utility

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ struct pair
9595
is_nothrow_swappable_v<T2>); // constexpr in C++20
9696
};
9797
98+
template<class T1, class T2, class U1, class U2, template<class> class TQual, template<class> class UQual>
99+
struct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual>; // since C++23
100+
101+
template<class T1, class T2, class U1, class U2>
102+
struct common_type<pair<T1, T2>, pair<U1, U2>>; // since C++23
103+
98104
template<class T1, class T2> pair(T1, T2) -> pair<T1, T2>;
99105
100106
template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14

libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_reference.compile.pass.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <tuple>
1717
#include <type_traits>
18+
#include <utility>
1819

1920
#include "test_macros.h"
2021

@@ -197,6 +198,25 @@ struct std::basic_common_reference<A, std::tuple<B>, TQual, UQual> {
197198

198199
static_assert(std::is_same_v<std::common_reference_t<A, std::tuple<B>,std::tuple<D>>, std::tuple<B>>);
199200

201+
202+
static_assert(std::is_same_v<std::common_reference_t<std::pair<int, int>>,
203+
std::pair<int, int>>);
204+
static_assert(std::is_same_v<std::common_reference_t<std::pair<int, long>, std::pair<long, int>>,
205+
std::pair<long, long>>);
206+
static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, const int&>, std::pair<const int&, int>>,
207+
std::pair<const int&, int>>);
208+
static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, volatile int&>, std::pair<volatile int&, int>>,
209+
std::pair<volatile int&, int>>);
210+
static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, const volatile int&>, std::pair<const volatile int&, int>>,
211+
std::pair<const volatile int&, int>>);
212+
static_assert(!has_type<std::common_reference_t<std::pair<const int&, volatile int&>,
213+
std::pair<volatile int&, const int&>>>);
214+
215+
static_assert(std::is_same_v<std::common_reference_t<std::pair<int, X2>, std::pair<int, Y2>>, std::pair<int, Z2>>);
216+
static_assert(std::is_same_v<std::common_reference_t<std::pair<int, X2>, std::pair<int, Y2>>, std::pair<int, Z2>>);
217+
static_assert(!has_type<std::common_reference<std::pair<int, const X2>, std::pair<float, const Z2>>>);
218+
static_assert(!has_type<std::common_reference<std::pair<int, X2>, std::pair<float, Z2>>>);
219+
static_assert(!has_type<std::common_reference<std::pair<int, X2>, int, X2>>);
200220
#endif
201221

202222
int main(int, char**) { return 0; }

libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <memory>
1515
#include <tuple>
1616
#include <type_traits>
17+
#include <utility>
1718

1819
#include "test_macros.h"
1920

@@ -234,11 +235,11 @@ void test_bullet_three_four() {
234235
static_assert(std::is_same_v<std::common_type_t<const bad_reference_wrapper<double>, double>, double>, "");
235236
static_assert(std::is_same_v<std::common_type_t<volatile bad_reference_wrapper<double>, double>, double>, "");
236237
static_assert(std::is_same_v<std::common_type_t<const volatile bad_reference_wrapper<double>, double>, double>, "");
237-
238+
238239
static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>, const double>, double>, "");
239240
static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>, volatile double>, double>, "");
240241
static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>, const volatile double>, double>, "");
241-
242+
242243
static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>&, double>, double>, "");
243244
static_assert(std::is_same_v<std::common_type_t<bad_reference_wrapper<double>, double&>, double>, "");
244245
#endif
@@ -359,7 +360,20 @@ int main(int, char**)
359360
static_assert(std::is_same_v<std::common_type_t<std::tuple<const int&>>, std::tuple<int>>);
360361
static_assert(std::is_same_v<std::common_type_t<std::tuple<const volatile int&>, std::tuple<const volatile long&>>, std::tuple<long>>);
361362

362-
static_assert(std::is_same_v<std::common_reference_t<A, std::tuple<B>,std::tuple<C>>, std::tuple<B>>);
363+
static_assert(std::is_same_v<std::common_type_t<A, std::tuple<B>,std::tuple<C>>, std::tuple<B>>);
364+
365+
static_assert(std::is_same_v<std::common_type_t<std::pair<int, int>>, std::pair<int, int>>);
366+
static_assert(std::is_same_v<std::common_type_t<std::pair<int, long>, std::pair<long, int>>, std::pair<long, long>>);
367+
static_assert(std::is_same_v<std::common_type_t<std::pair<const int, const long>,
368+
std::pair<const int, const long>>,
369+
std::pair<int, long>>);
370+
371+
static_assert(std::is_same_v<std::common_type_t<std::pair<const int&, const long&>>, std::pair<int, long>>);
372+
static_assert(std::is_same_v<std::common_type_t<std::pair<const volatile int&, const volatile long&>,
373+
std::pair<const volatile long&, const volatile int&>>,
374+
std::pair<long, long>>);
375+
376+
static_assert(std::is_same_v<std::common_type_t<A, std::tuple<B>,std::tuple<C>>, std::tuple<B>>);
363377
#endif
364378

365379
return 0;

0 commit comments

Comments
 (0)