Hi all, This patch adds support for the c++26 paper "P2835R7 Expose std::atomic_ref's object address", including test cases and feature test macro. Please take a look when you have time, thanks!
Best regards, Yuao
From 4d6321dd116ac9ea0a5307b745966049926e29a7 Mon Sep 17 00:00:00 2001 From: Yuao Ma <[email protected]> Date: Wed, 8 Oct 2025 01:16:02 +0800 Subject: [PATCH] libstdc++: Implement P2835R7 Expose std::atomic_ref's object address This patch adds the address function to __atomic_ref for all 4 variants. libstdc++-v3/ChangeLog: * include/bits/atomic_base.h: Implement address(). * include/bits/version.def: Bump version number. * include/bits/version.h: Regenerate. * testsuite/29_atomics/atomic_ref/address.cc: New test. --- libstdc++-v3/include/bits/atomic_base.h | 24 ++++++++++++++ libstdc++-v3/include/bits/version.def | 4 +++ libstdc++-v3/include/bits/version.h | 7 ++++- .../29_atomics/atomic_ref/address.cc | 31 +++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_ref/address.cc diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h index 92d1269493f..d9bcdc9ff8f 100644 --- a/libstdc++-v3/include/bits/atomic_base.h +++ b/libstdc++-v3/include/bits/atomic_base.h @@ -1625,6 +1625,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // TODO add const volatile overload #endif // __glibcxx_atomic_wait +#if __glibcxx_atomic_ref >= 202411L + _GLIBCXX_ALWAYS_INLINE constexpr _Tp* + address() const noexcept + { return _M_ptr; } +#endif // __glibcxx_atomic_ref >= 202411L + private: _Tp* _M_ptr; }; @@ -1740,6 +1746,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // TODO add const volatile overload #endif // __glibcxx_atomic_wait +#if __glibcxx_atomic_ref >= 202411L + _GLIBCXX_ALWAYS_INLINE constexpr _Tp* + address() const noexcept + { return _M_ptr; } +#endif // __glibcxx_atomic_ref >= 202411L + value_type fetch_add(value_type __i, memory_order __m = memory_order_seq_cst) const noexcept @@ -1915,6 +1927,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // TODO add const volatile overload #endif // __glibcxx_atomic_wait +#if __glibcxx_atomic_ref >= 202411L + _GLIBCXX_ALWAYS_INLINE constexpr _Fp* + address() const noexcept + { return _M_ptr; } +#endif // __glibcxx_atomic_ref >= 202411L + value_type fetch_add(value_type __i, memory_order __m = memory_order_seq_cst) const noexcept @@ -2044,6 +2062,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // TODO add const volatile overload #endif // __glibcxx_atomic_wait +#if __glibcxx_atomic_ref >= 202411L + _GLIBCXX_ALWAYS_INLINE constexpr _Tp** + address() const noexcept + { return _M_ptr; } +#endif // __glibcxx_atomic_ref >= 202411L + _GLIBCXX_ALWAYS_INLINE value_type fetch_add(difference_type __d, memory_order __m = memory_order_seq_cst) const noexcept diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index 3a26234f87e..edf1e237963 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -789,6 +789,10 @@ ftms = { ftms = { name = atomic_ref; + values = { + v = 202411; + cxxmin = 26; + }; values = { v = 201806; cxxmin = 20; diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index 46e4c1121e7..8d0531b2587 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -880,7 +880,12 @@ #undef __glibcxx_want_atomic_lock_free_type_aliases #if !defined(__cpp_lib_atomic_ref) -# if (__cplusplus >= 202002L) +# if (__cplusplus > 202302L) +# define __glibcxx_atomic_ref 202411L +# if defined(__glibcxx_want_all) || defined(__glibcxx_want_atomic_ref) +# define __cpp_lib_atomic_ref 202411L +# endif +# elif (__cplusplus >= 202002L) # define __glibcxx_atomic_ref 201806L # if defined(__glibcxx_want_all) || defined(__glibcxx_want_atomic_ref) # define __cpp_lib_atomic_ref 201806L diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_ref/address.cc b/libstdc++-v3/testsuite/29_atomics/atomic_ref/address.cc new file mode 100644 index 00000000000..1996ca5850c --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic_ref/address.cc @@ -0,0 +1,31 @@ +// { dg-do run { target c++26 } } +// { dg-require-atomic-cmpxchg-word "" } +// { dg-add-options libatomic } + +#include <atomic> +#include <memory> + +#include <testsuite_hooks.h> + +template <typename T> +struct TestAtomicRefAddress +{ + void operator()() const + { + T x(T(42)); + const std::atomic_ref<T> a(x); + + static_assert(noexcept(a.address())); + VERIFY( std::addressof(x) == a.address() ); + } +}; + +int +main () +{ + struct X { int c; }; + TestAtomicRefAddress<X>()(); + TestAtomicRefAddress<int>()(); + TestAtomicRefAddress<float>()(); + TestAtomicRefAddress<char *>()(); +} -- 2.43.0
