Skip to content

[libc++] Make list constexpr as part of P3372R3 #129799

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 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libcxx/docs/FeatureTestMacroTable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ Status
---------------------------------------------------------- -----------------
``__cpp_lib_constexpr_forward_list`` ``202502L``
---------------------------------------------------------- -----------------
``__cpp_lib_constexpr_list`` ``202502L``
---------------------------------------------------------- -----------------
``__cpp_lib_constexpr_new`` ``202406L``
---------------------------------------------------------- -----------------
``__cpp_lib_constexpr_queue`` ``202502L``
Expand Down
549 changes: 322 additions & 227 deletions libcxx/include/list

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions libcxx/include/version
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ __cpp_lib_constexpr_dynamic_alloc 201907L <memory>
__cpp_lib_constexpr_forward_list 202502L <forward_list>
__cpp_lib_constexpr_functional 201907L <functional>
__cpp_lib_constexpr_iterator 201811L <iterator>
__cpp_lib_constexpr_list 202502L <list>
__cpp_lib_constexpr_memory 202202L <memory>
201811L // C++20
__cpp_lib_constexpr_new 202406L <new>
Expand Down Expand Up @@ -545,6 +546,7 @@ __cpp_lib_void_t 201411L <type_traits>
# undef __cpp_lib_constexpr_algorithms
# define __cpp_lib_constexpr_algorithms 202306L
# define __cpp_lib_constexpr_forward_list 202502L
# define __cpp_lib_constexpr_list 202502L
# if !defined(_LIBCPP_ABI_VCRUNTIME)
# define __cpp_lib_constexpr_new 202406L
# endif
Expand Down
24 changes: 17 additions & 7 deletions libcxx/test/std/containers/sequences/list/compare.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@

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

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

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

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

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

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

#include <list>
#include <cassert>

#include "test_comparisons.h"

int main(int, char**) {
TEST_CONSTEXPR_CXX26 bool test() {
{
const std::list<int> l1, l2;
assert(testComparisons(l1, l2, true, false));
Expand Down Expand Up @@ -113,5 +113,15 @@ int main(int, char**) {
const std::list<LessAndEqComp> l2(items2, items2 + 2);
assert(testComparisons(l1, l2, false, false));
}

return true;
}

int main(int, char**) {
assert(test());
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

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

#include <list>
#include <cassert>
Expand All @@ -20,6 +20,8 @@

int main(int, char**) {
assert(test_sequence_container_spaceship<std::list>());
// `std::list` is not constexpr, so no `static_assert` test here.
#if TEST_STD_VER >= 26
static_assert(test_sequence_container_spaceship<std::list>());
#endif
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

// class list

// allocator_type get_allocator() const
// allocator_type get_allocator() const // constexpr since C++26

#include <list>
#include <cassert>

#include "test_allocator.h"
#include "test_macros.h"

int main(int, char**) {
TEST_CONSTEXPR_CXX26 bool test() {
{
std::allocator<int> alloc;
const std::list<int> l(alloc);
Expand All @@ -30,5 +30,14 @@ int main(int, char**) {
assert(l.get_allocator() == alloc);
}

return true;
}

int main(int, char**) {
assert(test());
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// type.

#include <list>
#include <cassert>

#include "test_macros.h"

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

int main(int, char**) {
TEST_CONSTEXPR_CXX26 bool test() {
A a;
(void)a;

return true;
}

int main(int, char**) {
assert(test());
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
27 changes: 20 additions & 7 deletions libcxx/test/std/containers/sequences/list/iterators.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

// <list>

// iterator begin();
// iterator end();
// const_iterator begin() const;
// const_iterator end() const;
// const_iterator cbegin() const;
// const_iterator cend() const;
// iterator begin(); // constexpr since C++26
// iterator end(); // constexpr since C++26
// const_iterator begin() const; // constexpr since C++26
// const_iterator end() const; // constexpr since C++26
// const_iterator cbegin() const; // constexpr since C++26
// const_iterator cend() const; // constexpr since C++26

#include <list>
#include <cassert>
Expand All @@ -27,7 +27,7 @@ struct A {
int second;
};

int main(int, char**) {
TEST_CONSTEXPR_CXX26 bool test() {
{
typedef int T;
typedef std::list<T> C;
Expand Down Expand Up @@ -74,6 +74,8 @@ int main(int, char**) {
typedef std::list<T> C;
C::iterator i;
C::const_iterator j;
(void)i;
(void)j;
}
#if TEST_STD_VER >= 11
{
Expand Down Expand Up @@ -122,6 +124,8 @@ int main(int, char**) {
typedef std::list<T, min_allocator<T>> C;
C::iterator i;
C::const_iterator j;
(void)i;
(void)j;
}
{
typedef A T;
Expand Down Expand Up @@ -150,5 +154,14 @@ int main(int, char**) {
}
#endif

return true;
}

int main(int, char**) {
assert(test());
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

// class list

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

#include <list>
#include <cassert>

#include "test_macros.h"
#include "min_allocator.h"

int main(int, char**) {
TEST_CONSTEXPR_CXX26 bool test() {
{
typedef std::list<int> C;
C c;
Expand All @@ -42,5 +42,14 @@ int main(int, char**) {
}
#endif

return true;
}

int main(int, char**) {
assert(test());
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// <list>

// size_type max_size() const noexcept
// size_type max_size() const noexcept // constexpr since C++26

#include <cassert>
#include <limits>
Expand All @@ -18,7 +18,7 @@
#include "test_allocator.h"
#include "test_macros.h"

int main(int, char**) {
TEST_CONSTEXPR_CXX26 bool test() {
{
typedef limited_allocator<int, 10> A;
typedef std::list<int, A> C;
Expand All @@ -42,5 +42,14 @@ int main(int, char**) {
assert(c.max_size() <= alloc_max_size(c.get_allocator()));
}

return true;
}

int main(int, char**) {
assert(test());
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

// <list>

// void resize(size_type sz);
// void resize(size_type sz); // constexpr since C++26

#include <list>
#include <cassert>

#include "test_macros.h"
#include "DefaultOnly.h"
#include "min_allocator.h"

int main(int, char**) {
TEST_CONSTEXPR_CXX26 bool test() {
{
std::list<int> l(5, 2);
l.resize(2);
Expand All @@ -33,17 +34,31 @@ int main(int, char**) {
assert(l.back() == 0);
}
#if TEST_STD_VER >= 11
{
std::list<DefaultOnly> l(10);
l.resize(5);
assert(l.size() == 5);
assert(std::distance(l.begin(), l.end()) == 5);
}
{
std::list<DefaultOnly> l(10);
l.resize(20);
assert(l.size() == 20);
assert(std::distance(l.begin(), l.end()) == 20);
if (!TEST_IS_CONSTANT_EVALUATED) {
{
std::list<DefaultOnly> l(10);
l.resize(5);
assert(l.size() == 5);
assert(std::distance(l.begin(), l.end()) == 5);
}
{
std::list<DefaultOnly> l(10);
l.resize(20);
assert(l.size() == 20);
assert(std::distance(l.begin(), l.end()) == 20);
}
{
std::list<DefaultOnly, min_allocator<DefaultOnly>> l(10);
l.resize(5);
assert(l.size() == 5);
assert(std::distance(l.begin(), l.end()) == 5);
}
{
std::list<DefaultOnly, min_allocator<DefaultOnly>> l(10);
l.resize(20);
assert(l.size() == 20);
assert(std::distance(l.begin(), l.end()) == 20);
}
}
{
std::list<int, min_allocator<int>> l(5, 2);
Expand All @@ -60,18 +75,15 @@ int main(int, char**) {
assert(l.front() == 2);
assert(l.back() == 0);
}
{
std::list<DefaultOnly, min_allocator<DefaultOnly>> l(10);
l.resize(5);
assert(l.size() == 5);
assert(std::distance(l.begin(), l.end()) == 5);
}
{
std::list<DefaultOnly, min_allocator<DefaultOnly>> l(10);
l.resize(20);
assert(l.size() == 20);
assert(std::distance(l.begin(), l.end()) == 20);
}
#endif

return true;
}

int main(int, char**) {
assert(test());
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
Expand Down
Loading
Loading