Hello,

The attached patch builds on top of the previous one, this time adding support for C++26's std::atomic_ref::address(). Tested on x86-64 Linux.

Thank you,
--
Giuseppe D'Angelo
From 0cc16c6e0365308255e02328b0d1f52c0624f8ac Mon Sep 17 00:00:00 2001
From: Giuseppe D'Angelo <giuseppe.dang...@kdab.com>
Date: Thu, 19 Dec 2024 18:39:20 +0100
Subject: [PATCH] libstdc++: add atomic_ref::address() (P2835R7)

This commit adds support for the address() getter to atomic_ref,
added by P2835R7 for C++26. The implementation is straightforward, just
return the value of the existing data member.

Technically speaking, P2835R7 has been rebased on top of P3309R3
(constexpr atomic and atomic_ref), marking address() as constexpr.
However P3309R3 has not been implemented yet in libstdc++ -- I'd wager
that adding constexpr to address() can be done at the same time as the
rest of the API. (Also, this particular function doesn't require
anything special in terms of implementation for it to be marked
`constexpr`, this is actually forbidden by [constexpr.functions] so I've
decided not to do it.)

libstdc++-v3/ChangeLog:

	* include/bits/atomic_base.h: Add address() to the various
	specializations of __atomic_ref_base.
	* include/bits/version.def: Bump the feature-testing macro.
	* include/bits/version.h: Regenerate.
	* testsuite/29_atomics/atomic_ref/address.cc: New test.

Signed-off-by: Giuseppe D'Angelo <giuseppe.dang...@kdab.com>
---
 libstdc++-v3/include/bits/atomic_base.h       | 16 ++++++
 libstdc++-v3/include/bits/version.def         |  4 ++
 libstdc++-v3/include/bits/version.h           |  7 ++-
 .../29_atomics/atomic_ref/address.cc          | 55 +++++++++++++++++++
 4 files changed, 81 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 df642716ce8..0c57f7b59ae 100644
--- a/libstdc++-v3/include/bits/atomic_base.h
+++ b/libstdc++-v3/include/bits/atomic_base.h
@@ -1556,6 +1556,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // TODO add const volatile overload
 #endif // __glibcxx_atomic_wait
 
+#if __glibcxx_atomic_ref >= 202411L // C++26
+      _Tp* address() const noexcept { return _M_ptr; }
+#endif
+
     protected:
       _Tp* _M_ptr;
     };
@@ -1690,6 +1694,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // TODO add const volatile overload
 #endif // __glibcxx_atomic_wait
 
+#if __glibcxx_atomic_ref >= 202411L // C++26
+      _Tp* address() const noexcept { return _M_ptr; }
+#endif
+
     protected:
       _Tp* _M_ptr;
     };
@@ -1883,6 +1891,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // TODO add const volatile overload
 #endif // __glibcxx_atomic_wait
 
+#if __glibcxx_atomic_ref >= 202411L // C++26
+      _Fp* address() const noexcept { return _M_ptr; }
+#endif
+
     protected:
       _Fp* _M_ptr;
     };
@@ -2032,6 +2044,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // TODO add const volatile overload
 #endif // __glibcxx_atomic_wait
 
+#if __glibcxx_atomic_ref >= 202411L // C++26
+      _Tp* address() const noexcept { return _M_ptr; }
+#endif
+
     protected:
       static constexpr ptrdiff_t
       _S_type_size(ptrdiff_t __d) noexcept
diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def
index 62b8252e02d..08ca01803c0 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -754,6 +754,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 16cdae920a1..714235a599c 100644
--- a/libstdc++-v3/include/bits/version.h
+++ b/libstdc++-v3/include/bits/version.h
@@ -841,7 +841,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..24cd6a2d877
--- /dev/null
+++ b/libstdc++-v3/testsuite/29_atomics/atomic_ref/address.cc
@@ -0,0 +1,55 @@
+// { dg-do run { target c++26 } }
+
+#include <atomic>
+#include <type_traits>
+#include <testsuite_hooks.h>
+
+#if !defined(__cpp_lib_atomic_ref)
+#error "__cpp_lib_atomic_ref should have been defined"
+#elif __cpp_lib_atomic_ref < 202411
+#error "__cpp_lib_atomic_ref should have been defined to >= 202411"
+#endif
+
+struct X
+{
+  X() = default;
+  X(int i) : i(i) { }
+  bool operator==(int rhs) const { return i == rhs; }
+  int i;
+};
+
+template<typename T>
+void
+test()
+{
+  T obj = {};
+
+  std::atomic_ref<T> ref(obj);
+  static_assert(std::is_same_v<decltype(ref.address()), T *>);
+  VERIFY(ref.address() == &obj);
+
+  const std::atomic_ref<T> ref2(obj);
+  static_assert(std::is_same_v<decltype(ref2.address()), T *>);
+  VERIFY(ref2.address() == &obj);
+}
+
+int
+main()
+{
+  test<int>();
+  test<const int>();
+  test<volatile int>();
+
+  test<float>();
+  test<const float>();
+  test<volatile float>();
+
+  test<int *>();
+  test<const int *>();
+  test<int * const>();
+  test<int * volatile>();
+
+  test<X>();
+  test<const X>();
+  test<volatile X>();
+}
-- 
2.34.1

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to