Skip to content

Commit 8e4c03d

Browse files
committed
Make list constexpr as part of P3372R3
1 parent eaade38 commit 8e4c03d

File tree

72 files changed

+1323
-647
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1323
-647
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ Status
416416
---------------------------------------------------------- -----------------
417417
``__cpp_lib_bitset`` ``202306L``
418418
---------------------------------------------------------- -----------------
419+
``__cpp_lib_constexpr_list`` ``202502L``
420+
---------------------------------------------------------- -----------------
419421
``__cpp_lib_constexpr_new`` ``202406L``
420422
---------------------------------------------------------- -----------------
421423
``__cpp_lib_constrained_equality`` *unimplemented*

libcxx/include/__memory/pointer_traits.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ concept __resettable_smart_pointer_with_args = requires(_Smart __s, _Pointer __p
302302

303303
#endif
304304

305+
template <class _PtrTo, class _PtrFrom>
306+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _PtrTo __static_fancy_pointer_cast(const _PtrFrom& __p) {
307+
using __ptr_traits = pointer_traits<_PtrTo>;
308+
using __element_type = typename __ptr_traits::element_type;
309+
return __p ? __ptr_traits::pointer_to(*static_cast<__element_type*>(std::addressof(*__p)))
310+
: static_cast<_PtrTo>(nullptr);
311+
}
312+
305313
_LIBCPP_END_NAMESPACE_STD
306314

307315
_LIBCPP_POP_MACROS

libcxx/include/list

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ struct __list_node_pointer_traits {
294294

295295
static _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __base_pointer
296296
__unsafe_link_pointer_cast(__node_pointer __p) {
297-
return static_cast<__base_pointer>(static_cast<_VoidPtr>(__p));
297+
return std::__static_fancy_pointer_cast<__base_pointer>(__p);
298298
}
299299
};
300300

@@ -318,7 +318,8 @@ struct __list_node_base {
318318
}
319319

320320
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __node_pointer __as_node() {
321-
return static_cast<__node_pointer>(__self());
321+
return pointer_traits<__node_pointer>::pointer_to(
322+
*static_cast<typename pointer_traits<__node_pointer>::element_type*>(this));
322323
}
323324
};
324325

@@ -795,8 +796,10 @@ public:
795796
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
796797

797798
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return this->__size_; }
798-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __base::empty(); }
799-
_LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
799+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
800+
return __base::empty();
801+
}
802+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
800803
return std::min<size_type>(this->__node_alloc_max_size(), numeric_limits<difference_type >::max());
801804
}
802805

libcxx/include/version

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ __cpp_lib_constexpr_complex 201711L <complex>
6868
__cpp_lib_constexpr_dynamic_alloc 201907L <memory>
6969
__cpp_lib_constexpr_functional 201907L <functional>
7070
__cpp_lib_constexpr_iterator 201811L <iterator>
71+
__cpp_lib_constexpr_list 202502L <list>
7172
__cpp_lib_constexpr_memory 202202L <memory>
7273
201811L // C++20
7374
__cpp_lib_constexpr_new 202406L <new>
@@ -536,6 +537,7 @@ __cpp_lib_void_t 201411L <type_traits>
536537
# undef __cpp_lib_bind_front
537538
# define __cpp_lib_bind_front 202306L
538539
# define __cpp_lib_bitset 202306L
540+
# define __cpp_lib_constexpr_list 202502L
539541
# if !defined(_LIBCPP_ABI_VCRUNTIME)
540542
# define __cpp_lib_constexpr_new 202406L
541543
# endif

libcxx/test/std/containers/sequences/list/compare.pass.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,34 @@
1010

1111
// template< class T, class Alloc >
1212
// bool operator==( const std::list<T,Alloc>& lhs,
13-
// const std::list<T,Alloc>& rhs );
13+
// const std::list<T,Alloc>& rhs ); // constexpr since C++26
1414

1515
// template< class T, class Alloc >
1616
// bool operator!=( const std::list<T,Alloc>& lhs,
17-
// const std::list<T,Alloc>& rhs );
17+
// const std::list<T,Alloc>& rhs ); // constexpr since C++26
1818

1919
// template< class T, class Alloc >
2020
// bool operator<( const std::list<T,Alloc>& lhs,
21-
// const std::list<T,Alloc>& rhs );
21+
// const std::list<T,Alloc>& rhs ); // constexpr since C++26
2222

2323
// template< class T, class Alloc >
2424
// bool operator<=( const std::list<T,Alloc>& lhs,
25-
// const std::list<T,Alloc>& rhs );
25+
// const std::list<T,Alloc>& rhs ); // constexpr since C++26
2626

2727
// template< class T, class Alloc >
2828
// bool operator>( const std::list<T,Alloc>& lhs,
29-
// const std::list<T,Alloc>& rhs );
29+
// const std::list<T,Alloc>& rhs ); // constexpr since C++26
3030

3131
// template< class T, class Alloc >
3232
// bool operator>=( const std::list<T,Alloc>& lhs,
33-
// const std::list<T,Alloc>& rhs );
33+
// const std::list<T,Alloc>& rhs ); // constexpr since C++26
3434

3535
#include <list>
3636
#include <cassert>
3737

3838
#include "test_comparisons.h"
3939

40-
int main(int, char**) {
40+
TEST_CONSTEXPR_CXX26 bool test() {
4141
{
4242
const std::list<int> l1, l2;
4343
assert(testComparisons(l1, l2, true, false));
@@ -113,5 +113,15 @@ int main(int, char**) {
113113
const std::list<LessAndEqComp> l2(items2, items2 + 2);
114114
assert(testComparisons(l1, l2, false, false));
115115
}
116+
117+
return true;
118+
}
119+
120+
int main(int, char**) {
121+
assert(test());
122+
#if TEST_STD_VER >= 26
123+
static_assert(test());
124+
#endif
125+
116126
return 0;
117127
}

libcxx/test/std/containers/sequences/list/compare.three_way.pass.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
// template <class T, class Allocator> constexpr
1313
// synth-three-way-result<T>
14-
// operator<=>(const list<T, Allocator>& x, const list<T, Allocator>& y);
14+
// operator<=>(const list<T, Allocator>& x, const list<T, Allocator>& y); // constexpr since C++26
1515

1616
#include <list>
1717
#include <cassert>
@@ -20,6 +20,8 @@
2020

2121
int main(int, char**) {
2222
assert(test_sequence_container_spaceship<std::list>());
23-
// `std::list` is not constexpr, so no `static_assert` test here.
23+
#if TEST_STD_VER >= 26
24+
static_assert(test_sequence_container_spaceship<std::list>());
25+
#endif
2426
return 0;
2527
}

libcxx/test/std/containers/sequences/list/get_allocator.pass.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
// class list
1212

13-
// allocator_type get_allocator() const
13+
// allocator_type get_allocator() const // constexpr since C++26
1414

1515
#include <list>
1616
#include <cassert>
1717

1818
#include "test_allocator.h"
1919
#include "test_macros.h"
2020

21-
int main(int, char**) {
21+
TEST_CONSTEXPR_CXX26 bool test() {
2222
{
2323
std::allocator<int> alloc;
2424
const std::list<int> l(alloc);
@@ -30,5 +30,14 @@ int main(int, char**) {
3030
assert(l.get_allocator() == alloc);
3131
}
3232

33+
return true;
34+
}
35+
36+
int main(int, char**) {
37+
assert(test());
38+
#if TEST_STD_VER >= 26
39+
static_assert(test());
40+
#endif
41+
3342
return 0;
3443
}

libcxx/test/std/containers/sequences/list/incomplete_type.pass.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// type.
1313

1414
#include <list>
15+
#include <cassert>
1516

1617
#include "test_macros.h"
1718

@@ -23,8 +24,18 @@ struct A {
2324
std::list<A>::const_reverse_iterator crit;
2425
};
2526

26-
int main(int, char**) {
27+
TEST_CONSTEXPR_CXX26 bool test() {
2728
A a;
29+
(void)a;
30+
31+
return true;
32+
}
33+
34+
int main(int, char**) {
35+
assert(test());
36+
#if TEST_STD_VER >= 26
37+
static_assert(test());
38+
#endif
2839

2940
return 0;
3041
}

libcxx/test/std/containers/sequences/list/iterators.pass.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
// <list>
1010

11-
// iterator begin();
12-
// iterator end();
13-
// const_iterator begin() const;
14-
// const_iterator end() const;
15-
// const_iterator cbegin() const;
16-
// const_iterator cend() const;
11+
// iterator begin(); // constexpr since C++26
12+
// iterator end(); // constexpr since C++26
13+
// const_iterator begin() const; // constexpr since C++26
14+
// const_iterator end() const; // constexpr since C++26
15+
// const_iterator cbegin() const; // constexpr since C++26
16+
// const_iterator cend() const; // constexpr since C++26
1717

1818
#include <list>
1919
#include <cassert>
@@ -27,7 +27,7 @@ struct A {
2727
int second;
2828
};
2929

30-
int main(int, char**) {
30+
TEST_CONSTEXPR_CXX26 bool test() {
3131
{
3232
typedef int T;
3333
typedef std::list<T> C;
@@ -74,6 +74,8 @@ int main(int, char**) {
7474
typedef std::list<T> C;
7575
C::iterator i;
7676
C::const_iterator j;
77+
(void)i;
78+
(void)j;
7779
}
7880
#if TEST_STD_VER >= 11
7981
{
@@ -122,6 +124,8 @@ int main(int, char**) {
122124
typedef std::list<T, min_allocator<T>> C;
123125
C::iterator i;
124126
C::const_iterator j;
127+
(void)i;
128+
(void)j;
125129
}
126130
{
127131
typedef A T;
@@ -150,5 +154,14 @@ int main(int, char**) {
150154
}
151155
#endif
152156

157+
return true;
158+
}
159+
160+
int main(int, char**) {
161+
assert(test());
162+
#if TEST_STD_VER >= 26
163+
static_assert(test());
164+
#endif
165+
153166
return 0;
154167
}

libcxx/test/std/containers/sequences/list/list.capacity/empty.pass.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
// class list
1212

13-
// bool empty() const noexcept;
13+
// bool empty() const noexcept; // constexpr since C++26
1414

1515
#include <list>
1616
#include <cassert>
1717

1818
#include "test_macros.h"
1919
#include "min_allocator.h"
2020

21-
int main(int, char**) {
21+
TEST_CONSTEXPR_CXX26 bool test() {
2222
{
2323
typedef std::list<int> C;
2424
C c;
@@ -42,5 +42,14 @@ int main(int, char**) {
4242
}
4343
#endif
4444

45+
return true;
46+
}
47+
48+
int main(int, char**) {
49+
assert(test());
50+
#if TEST_STD_VER >= 26
51+
static_assert(test());
52+
#endif
53+
4554
return 0;
4655
}

0 commit comments

Comments
 (0)