[libcxx-commits] [libcxx] [libc++] Make list constexpr as part of P3372R3 (PR #129799)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jun 11 07:06:04 PDT 2025


================
@@ -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) {
----------------
winner245 wrote:

The `DefaultOnly` constructor cannot be marked `constexpr` because its constructor modifies a static data member  `count`, which starts life time outside the constructor. 

```cpp
    static int count;


    DefaultOnly() : data_(-1) {++count;}
    ~DefaultOnly() {data_ = 0; --count;}
```

Thus, the constructor call to `DefaultOnly` is not a [core constant expression](https://en.cppreference.com/w/cpp/language/constant_expression.html). This makes the compile-time evaluation involving `DefaultOnly` not possible. 

https://github.com/llvm/llvm-project/pull/129799


More information about the libcxx-commits mailing list