[gcc r17-146] libstdc++: Make pointer_traits::pointer_to constexpr for main template.

Tomasz Kaminski tkaminsk@gcc.gnu.org
Tue Apr 28 15:14:30 GMT 2026


https://gcc.gnu.org/g:94b37910f620ecafe6526bfd37722a86517c3f0d

commit r17-146-g94b37910f620ecafe6526bfd37722a86517c3f0d
Author: Tomasz Kamiński <tkaminsk@redhat.com>
Date:   Tue Apr 28 16:01:47 2026 +0200

    libstdc++: Make pointer_traits::pointer_to constexpr for main template.
    
    This resolves LWG3454, "pointer_traits::pointer_to should be constexpr",
    accepted in Kona 2025.
    
    The change is applied since C++20, i.e. standard in which pointer_to
    was made constexpr for T* specialization.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/ptr_traits.h (__ptr_traits_ptr_to::pointer_to):
            Define as constexpr since C++20.
            * testsuite/20_util/pointer_traits/pointer_to_constexpr.cc:
            New test for custom pointer-like type.

Diff:
---
 libstdc++-v3/include/bits/ptr_traits.h                     |  4 +++-
 .../20_util/pointer_traits/pointer_to_constexpr.cc         | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h
index df83e0cad1d4..c1dc76509cee 100644
--- a/libstdc++-v3/include/bits/ptr_traits.h
+++ b/libstdc++-v3/include/bits/ptr_traits.h
@@ -97,13 +97,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       using pointer = _Ptr;
       using element_type = _Elt;
 
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // 3454. pointer_traits::pointer_to should be constexpr
       /**
        *  @brief  Obtain a pointer to an object
        *  @param  __r  A reference to an object of type `element_type`
        *  @return `pointer::pointer_to(__r)`
        *  @pre `pointer::pointer_to(__r)` is a valid expression.
       */
-      static pointer
+      static _GLIBCXX20_CONSTEXPR pointer
       pointer_to(element_type& __r)
 #if __cpp_lib_concepts
       requires requires {
diff --git a/libstdc++-v3/testsuite/20_util/pointer_traits/pointer_to_constexpr.cc b/libstdc++-v3/testsuite/20_util/pointer_traits/pointer_to_constexpr.cc
index cfd35f899c41..929d004fd4a1 100644
--- a/libstdc++-v3/testsuite/20_util/pointer_traits/pointer_to_constexpr.cc
+++ b/libstdc++-v3/testsuite/20_util/pointer_traits/pointer_to_constexpr.cc
@@ -24,3 +24,17 @@ static_assert( std::pointer_traits<int*>::pointer_to(i) == &i );
 
 struct X { } x;
 static_assert( std::pointer_traits<X*>::pointer_to(x) == &x );
+
+template<typename T>
+struct Ptr
+{
+  T* value;
+
+  constexpr static Ptr
+  pointer_to(T& t) { return Ptr{&t}; }
+
+  friend bool operator==(Ptr, Ptr) = default;
+};
+
+static_assert( std::pointer_traits<Ptr<int>>::pointer_to(i) == Ptr<int>{&i} );
+static_assert( std::pointer_traits<Ptr<X>>::pointer_to(x) == Ptr<X>{&x} );


More information about the Libstdc++-cvs mailing list