[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:17:34 PDT 2025


================
@@ -332,10 +338,14 @@ public:
   typedef __list_node_base<_Tp, _VoidPtr> __base;
   typedef typename __base::__base_pointer __base_pointer;
 
-  _LIBCPP_HIDE_FROM_ABI explicit __list_node(__base_pointer __prev, __base_pointer __next) : __base(__prev, __next) {}
-  _LIBCPP_HIDE_FROM_ABI ~__list_node() {}
+  _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI explicit __list_node(__base_pointer __prev, __base_pointer __next)
+      : __base(__prev, __next) {}
+  _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI ~__list_node() {}
 
-  _LIBCPP_HIDE_FROM_ABI __base_pointer __as_link() { return __base::__self(); }
+  _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __base_pointer __as_link() {
+    return pointer_traits<__base_pointer>::pointer_to(
+        *static_cast<typename pointer_traits<__base_pointer>::element_type*>(std::addressof(*this)));
----------------
winner245 wrote:

Yes, you are right, `std::addressof(*this) == this` given that `this` is a raw pointer (only for fancy pointers can `std::addressof(*p)` differ from `p`). I adopted your simplification, and then I realized that the cast expression can be further simplified as follows: 

`static_cast<typename pointer_traits<__base_pointer>::element_type*>(this) == static_cast<__base*>(this)`

So the entire function `__as_link()` simplifies to `return __base::__self();` (the original version we had) because inheritance would automatically cast `this` to a base `this` when calling a base member function. 

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


More information about the libcxx-commits mailing list