]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Implement P2835R7 Expose std::atomic_ref's object address
authorYuao Ma <c8ef@outlook.com>
Fri, 10 Oct 2025 15:14:48 +0000 (23:14 +0800)
committerc8ef <c8ef@outlook.com>
Mon, 13 Oct 2025 15:03:41 +0000 (23:03 +0800)
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.

libstdc++-v3/include/bits/atomic_base.h
libstdc++-v3/include/bits/version.def
libstdc++-v3/include/bits/version.h
libstdc++-v3/testsuite/29_atomics/atomic_ref/address.cc [new file with mode: 0644]

index 84661d449e25c390571e76ab9647c0c09f58d4fe..0f3f6b1925d3f72c656ae4fce416d5bc2aa5ad97 100644 (file)
@@ -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<typename _Tp,
index 83f1817bf8e4a54ede5b27638ff0532b2dab6be0..1118da4b541ea9e77e63ae67f7bc2c769b0d3e68 100644 (file)
@@ -789,6 +789,10 @@ ftms = {
 
 ftms = {
   name = atomic_ref;
+  values = {
+    v = 202411;
+    cxxmin = 26;
+  };
   values = {
     v = 201806;
     cxxmin = 20;
index 0d6692d244a675314a48a80d094adbee67287c81..c452bbeec8e02bb14735e5a7c6b1bbd63fda1189 100644 (file)
 #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 (file)
index 0000000..42a080a
--- /dev/null
@@ -0,0 +1,39 @@
+// { dg-do run { target c++26 } }
+// { dg-require-atomic-cmpxchg-word "" }
+// { dg-add-options libatomic }
+
+#include <atomic>
+#include <memory>
+#include <type_traits>
+
+#include <testsuite_hooks.h>
+
+template <typename T>
+void testAtomicRefAddress()
+{
+  T x(T(42));
+  const std::atomic_ref<T> a(x);
+
+  static_assert( noexcept(a.address()) );
+  static_assert( std::is_same_v<decltype(a.address()), T*> );
+  VERIFY( std::addressof(x) == a.address() );
+}
+
+template <typename T>
+void testAtomicRefAddressForCV()
+{
+  testAtomicRefAddress<T>();
+  testAtomicRefAddress<const T>();
+  testAtomicRefAddress<volatile T>();
+  testAtomicRefAddress<const volatile T>();
+}
+
+int
+main ()
+{
+  struct X { int c; };
+  testAtomicRefAddressForCV<X>();
+  testAtomicRefAddressForCV<int>();
+  testAtomicRefAddressForCV<float>();
+  testAtomicRefAddressForCV<char*>();
+}