From: Yuao Ma Date: Fri, 10 Oct 2025 15:14:48 +0000 (+0800) Subject: libstdc++: Implement P2835R7 Expose std::atomic_ref's object address X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c5e1093a729223c4e50a5fd28d67b9dfe73a87c;p=thirdparty%2Fgcc.git libstdc++: Implement P2835R7 Expose std::atomic_ref's object address This patch adds the address function to __atomic_ref_base. 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. --- diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h index 84661d449e2..0f3f6b1925d 100644 --- a/libstdc++-v3/include/bits/atomic_base.h +++ b/libstdc++-v3/include/bits/atomic_base.h @@ -1589,6 +1589,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } #endif // __glibcxx_atomic_wait +#if __glibcxx_atomic_ref >= 202411L + _GLIBCXX_ALWAYS_INLINE constexpr const _Tp* + address() const noexcept + { return _M_ptr; } +#endif // __glibcxx_atomic_ref >= 202411L + protected: _Tp* _M_ptr; }; @@ -1672,6 +1678,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __atomic_impl::notify_all(this->_M_ptr); } #endif // __glibcxx_atomic_wait + +#if __glibcxx_atomic_ref >= 202411L + _GLIBCXX_ALWAYS_INLINE constexpr _Tp* + address() const noexcept + { return this->_M_ptr; } +#endif // __glibcxx_atomic_ref >= 202411L }; template= 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..42a080af55e --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic_ref/address.cc @@ -0,0 +1,39 @@ +// { dg-do run { target c++26 } } +// { dg-require-atomic-cmpxchg-word "" } +// { dg-add-options libatomic } + +#include +#include +#include + +#include + +template +void testAtomicRefAddress() +{ + T x(T(42)); + const std::atomic_ref a(x); + + static_assert( noexcept(a.address()) ); + static_assert( std::is_same_v ); + VERIFY( std::addressof(x) == a.address() ); +} + +template +void testAtomicRefAddressForCV() +{ + testAtomicRefAddress(); + testAtomicRefAddress(); + testAtomicRefAddress(); + testAtomicRefAddress(); +} + +int +main () +{ + struct X { int c; }; + testAtomicRefAddressForCV(); + testAtomicRefAddressForCV(); + testAtomicRefAddressForCV(); + testAtomicRefAddressForCV(); +}