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.
This commit is contained in:
Tomasz Kamiński
2026-04-28 16:01:47 +02:00
parent 47480a30d0
commit 94b37910f6
2 changed files with 17 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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} );