[PATCH] libstdc++: Make pointer_traits::pointer_to constexpr for main template.
Tomasz Kamiński
tkaminsk@redhat.com
Tue Apr 28 14:40:33 GMT 2026
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.
---
Tested on x86_64-linux locally. OK for trunk?
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 df83e0cad1d..c1dc76509ce 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 cfd35f899c4..929d004fd4a 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} );
--
2.53.0
More information about the Libstdc++
mailing list