From: Tomasz KamiƄski Date: Tue, 28 Apr 2026 14:01:47 +0000 (+0200) Subject: libstdc++: Make pointer_traits::pointer_to constexpr for main template. X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;p=thirdparty%2Fgcc.git 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 --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::pointer_to(i) == &i ); struct X { } x; static_assert( std::pointer_traits::pointer_to(x) == &x ); + +template +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>::pointer_to(i) == Ptr{&i} ); +static_assert( std::pointer_traits>::pointer_to(x) == Ptr{&x} );