Skip to content

Commit 737a450

Browse files
committed
Add constexpr to pair
Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D80558
1 parent 5d1fe3f commit 737a450

14 files changed

+478
-377
lines changed

libcxx/docs/FeatureTestMacroTable.rst

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ Status
184184
------------------------------------------------- -----------------
185185
``__cpp_lib_constexpr_swap_algorithms`` *unimplemented*
186186
------------------------------------------------- -----------------
187+
``__cpp_lib_constexpr_utility `` ``201811L``
188+
------------------------------------------------- -----------------
187189
``__cpp_lib_destroying_delete`` ``201806L``
188190
------------------------------------------------- -----------------
189191
``__cpp_lib_endian`` ``201907L``

libcxx/include/tuple

+1-1
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ struct _LIBCPP_TEMPLATE_VIS uses_allocator<tuple<_Tp...>, _Alloc>
13931393

13941394
template <class _T1, class _T2>
13951395
template <class... _Args1, class... _Args2, size_t ..._I1, size_t ..._I2>
1396-
inline _LIBCPP_INLINE_VISIBILITY
1396+
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
13971397
pair<_T1, _T2>::pair(piecewise_construct_t,
13981398
tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
13991399
__tuple_indices<_I1...>, __tuple_indices<_I2...>)

libcxx/include/utility

+10-10
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ struct _LIBCPP_TEMPLATE_VIS pair
499499
second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
500500

501501
template <class... _Args1, class... _Args2>
502-
_LIBCPP_INLINE_VISIBILITY
502+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
503503
pair(piecewise_construct_t __pc,
504504
tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
505505
_NOEXCEPT_((is_nothrow_constructible<first_type, _Args1...>::value &&
@@ -508,7 +508,7 @@ struct _LIBCPP_TEMPLATE_VIS pair
508508
typename __make_tuple_indices<sizeof...(_Args1)>::type(),
509509
typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
510510

511-
_LIBCPP_INLINE_VISIBILITY
511+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
512512
pair& operator=(typename conditional<
513513
is_copy_assignable<first_type>::value &&
514514
is_copy_assignable<second_type>::value,
@@ -521,7 +521,7 @@ struct _LIBCPP_TEMPLATE_VIS pair
521521
return *this;
522522
}
523523

524-
_LIBCPP_INLINE_VISIBILITY
524+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
525525
pair& operator=(typename conditional<
526526
is_move_assignable<first_type>::value &&
527527
is_move_assignable<second_type>::value,
@@ -537,15 +537,15 @@ struct _LIBCPP_TEMPLATE_VIS pair
537537
template <class _Tuple, _EnableB<
538538
_CheckTLC<_Tuple>::template __enable_assign<_Tuple>()
539539
> = false>
540-
_LIBCPP_INLINE_VISIBILITY
540+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
541541
pair& operator=(_Tuple&& __p) {
542542
first = _VSTD::get<0>(_VSTD::forward<_Tuple>(__p));
543543
second = _VSTD::get<1>(_VSTD::forward<_Tuple>(__p));
544544
return *this;
545545
}
546546
#endif
547547

548-
_LIBCPP_INLINE_VISIBILITY
548+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
549549
void
550550
swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
551551
__is_nothrow_swappable<second_type>::value)
@@ -558,10 +558,10 @@ private:
558558

559559
#ifndef _LIBCPP_CXX03_LANG
560560
template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
561-
_LIBCPP_INLINE_VISIBILITY
562-
pair(piecewise_construct_t,
563-
tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
564-
__tuple_indices<_I1...>, __tuple_indices<_I2...>);
561+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
562+
pair(piecewise_construct_t,
563+
tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
564+
__tuple_indices<_I1...>, __tuple_indices<_I2...>);
565565
#endif
566566
};
567567

@@ -619,7 +619,7 @@ operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
619619
}
620620

621621
template <class _T1, class _T2>
622-
inline _LIBCPP_INLINE_VISIBILITY
622+
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
623623
typename enable_if
624624
<
625625
__is_swappable<_T1>::value &&

libcxx/include/version

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ __cpp_lib_concepts 201806L <concepts>
4242
__cpp_lib_constexpr_misc 201811L <array> <functional> <iterator>
4343
<string_view> <tuple> <utility>
4444
__cpp_lib_constexpr_swap_algorithms 201806L <algorithm>
45+
__cpp_lib_constexpr_utility 201811L <utility>
4546
__cpp_lib_destroying_delete 201806L <new>
4647
__cpp_lib_enable_shared_from_this 201603L <memory>
4748
__cpp_lib_endian 201907L <bit>
@@ -227,6 +228,7 @@ __cpp_lib_void_t 201411L <type_traits>
227228
// # define __cpp_lib_concepts 201806L
228229
// # define __cpp_lib_constexpr_misc 201811L
229230
// # define __cpp_lib_constexpr_swap_algorithms 201806L
231+
# define __cpp_lib_constexpr_utility 201811L
230232
# if _LIBCPP_STD_VER > 17 && defined(__cpp_impl_destroying_delete) && __cpp_impl_destroying_delete >= 201806L
231233
# define __cpp_lib_destroying_delete 201806L
232234
# endif

libcxx/test/std/language.support/support.limits/support.limits.general/utility.version.pass.cpp

+27-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
// Test the feature test macros defined by <utility>
1515

16-
/* Constant Value
17-
__cpp_lib_as_const 201510L [C++17]
18-
__cpp_lib_constexpr_misc 201811L [C++2a]
19-
__cpp_lib_exchange_function 201304L [C++14]
20-
__cpp_lib_integer_sequence 201304L [C++14]
21-
__cpp_lib_to_chars 201611L [C++17]
22-
__cpp_lib_tuples_by_type 201304L [C++14]
16+
/* Constant Value
17+
__cpp_lib_as_const 201510L [C++17]
18+
__cpp_lib_constexpr_misc 201811L [C++2a]
19+
__cpp_lib_constexpr_utility 201811L [C++2a]
20+
__cpp_lib_exchange_function 201304L [C++14]
21+
__cpp_lib_integer_sequence 201304L [C++14]
22+
__cpp_lib_to_chars 201611L [C++17]
23+
__cpp_lib_tuples_by_type 201304L [C++14]
2324
*/
2425

2526
#include <utility>
@@ -35,6 +36,10 @@
3536
# error "__cpp_lib_constexpr_misc should not be defined before c++2a"
3637
# endif
3738

39+
# ifdef __cpp_lib_constexpr_utility
40+
# error "__cpp_lib_constexpr_utility should not be defined before c++2a"
41+
# endif
42+
3843
# ifdef __cpp_lib_exchange_function
3944
# error "__cpp_lib_exchange_function should not be defined before c++14"
4045
# endif
@@ -61,6 +66,10 @@
6166
# error "__cpp_lib_constexpr_misc should not be defined before c++2a"
6267
# endif
6368

69+
# ifdef __cpp_lib_constexpr_utility
70+
# error "__cpp_lib_constexpr_utility should not be defined before c++2a"
71+
# endif
72+
6473
# ifndef __cpp_lib_exchange_function
6574
# error "__cpp_lib_exchange_function should be defined in c++14"
6675
# endif
@@ -99,6 +108,10 @@
99108
# error "__cpp_lib_constexpr_misc should not be defined before c++2a"
100109
# endif
101110

111+
# ifdef __cpp_lib_constexpr_utility
112+
# error "__cpp_lib_constexpr_utility should not be defined before c++2a"
113+
# endif
114+
102115
# ifndef __cpp_lib_exchange_function
103116
# error "__cpp_lib_exchange_function should be defined in c++17"
104117
# endif
@@ -155,6 +168,13 @@
155168
# endif
156169
# endif
157170

171+
# ifndef __cpp_lib_constexpr_utility
172+
# error "__cpp_lib_constexpr_utility should be defined in c++2a"
173+
# endif
174+
# if __cpp_lib_constexpr_utility != 201811L
175+
# error "__cpp_lib_constexpr_utility should have the value 201811L in c++2a"
176+
# endif
177+
158178
# ifndef __cpp_lib_exchange_function
159179
# error "__cpp_lib_exchange_function should be defined in c++2a"
160180
# endif

libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
__cpp_lib_concepts 201806L [C++2a]
3737
__cpp_lib_constexpr_misc 201811L [C++2a]
3838
__cpp_lib_constexpr_swap_algorithms 201806L [C++2a]
39+
__cpp_lib_constexpr_utility 201811L [C++2a]
3940
__cpp_lib_destroying_delete 201806L [C++2a]
4041
__cpp_lib_enable_shared_from_this 201603L [C++17]
4142
__cpp_lib_endian 201907L [C++2a]
@@ -194,6 +195,10 @@
194195
# error "__cpp_lib_constexpr_swap_algorithms should not be defined before c++2a"
195196
# endif
196197

198+
# ifdef __cpp_lib_constexpr_utility
199+
# error "__cpp_lib_constexpr_utility should not be defined before c++2a"
200+
# endif
201+
197202
# ifdef __cpp_lib_destroying_delete
198203
# error "__cpp_lib_destroying_delete should not be defined before c++2a"
199204
# endif
@@ -550,6 +555,10 @@
550555
# error "__cpp_lib_constexpr_swap_algorithms should not be defined before c++2a"
551556
# endif
552557

558+
# ifdef __cpp_lib_constexpr_utility
559+
# error "__cpp_lib_constexpr_utility should not be defined before c++2a"
560+
# endif
561+
553562
# ifdef __cpp_lib_destroying_delete
554563
# error "__cpp_lib_destroying_delete should not be defined before c++2a"
555564
# endif
@@ -1020,6 +1029,10 @@
10201029
# error "__cpp_lib_constexpr_swap_algorithms should not be defined before c++2a"
10211030
# endif
10221031

1032+
# ifdef __cpp_lib_constexpr_utility
1033+
# error "__cpp_lib_constexpr_utility should not be defined before c++2a"
1034+
# endif
1035+
10231036
# ifdef __cpp_lib_destroying_delete
10241037
# error "__cpp_lib_destroying_delete should not be defined before c++2a"
10251038
# endif
@@ -1721,6 +1734,13 @@
17211734
# endif
17221735
# endif
17231736

1737+
# ifndef __cpp_lib_constexpr_utility
1738+
# error "__cpp_lib_constexpr_utility should be defined in c++2a"
1739+
# endif
1740+
# if __cpp_lib_constexpr_utility != 201811L
1741+
# error "__cpp_lib_constexpr_utility should have the value 201811L in c++2a"
1742+
# endif
1743+
17241744
# if TEST_STD_VER > 17 && defined(__cpp_impl_destroying_delete) && __cpp_impl_destroying_delete >= 201806L
17251745
# ifndef __cpp_lib_destroying_delete
17261746
# error "__cpp_lib_destroying_delete should be defined in c++2a"

libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_const_pair_U_V.pass.cpp

+45-27
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,51 @@
2020
#include "archetypes.h"
2121
#endif
2222

23-
int main(int, char**)
24-
{
25-
{
26-
typedef std::pair<int, short> P1;
27-
typedef std::pair<double, long> P2;
28-
P1 p1(3, static_cast<short>(4));
29-
P2 p2;
30-
p2 = p1;
31-
assert(p2.first == 3);
32-
assert(p2.second == 4);
33-
}
34-
#if TEST_STD_VER >= 11
35-
{
36-
using C = TestTypes::TestType;
37-
using P = std::pair<int, C>;
38-
using T = std::pair<long, C>;
39-
const T t(42, -42);
40-
P p(101, 101);
41-
C::reset_constructors();
42-
p = t;
43-
assert(C::constructed == 0);
44-
assert(C::assigned == 1);
45-
assert(C::copy_assigned == 1);
46-
assert(C::move_assigned == 0);
47-
assert(p.first == 42);
48-
assert(p.second.value == -42);
49-
}
23+
TEST_CONSTEXPR_CXX20 bool test() {
24+
{
25+
typedef std::pair<int, short> P1;
26+
typedef std::pair<double, long> P2;
27+
P1 p1(3, static_cast<short>(4));
28+
P2 p2;
29+
p2 = p1;
30+
assert(p2.first == 3);
31+
assert(p2.second == 4);
32+
}
33+
#if TEST_STD_VER >= 20
34+
{
35+
using C = ConstexprTestTypes::TestType;
36+
using P = std::pair<int, C>;
37+
using T = std::pair<long, C>;
38+
const T t(42, -42);
39+
P p(101, 101);
40+
p = t;
41+
assert(p.first == 42);
42+
assert(p.second.value == -42);
43+
}
44+
#elif TEST_STD_VER >= 11
45+
{
46+
using C = TestTypes::TestType;
47+
using P = std::pair<int, C>;
48+
using T = std::pair<long, C>;
49+
const T t(42, -42);
50+
P p(101, 101);
51+
C::reset_constructors();
52+
p = t;
53+
assert(C::constructed == 0);
54+
assert(C::assigned == 1);
55+
assert(C::copy_assigned == 1);
56+
assert(C::move_assigned == 0);
57+
assert(p.first == 42);
58+
assert(p.second.value == -42);
59+
}
60+
#endif
61+
return true;
62+
}
63+
64+
int main(int, char**) {
65+
test();
66+
#if TEST_STD_VER >= 20
67+
static_assert(test());
5068
#endif
5169

5270
return 0;

0 commit comments

Comments
 (0)