[libcxx-commits] [libcxx] [libc++] Make `_Atomic(T)` directly use an alias template (PR #168679)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 18 23:27:47 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

<details>
<summary>Changes</summary>

Previously, libc++'s `_Atomic(T)` directly expanded to `::std::atomic<T>`, while the standard wording specified that it uses an alias template. The divergence was observable and made libc++ accept some invalid code like `struct _Atomic(T) t;`.

This patch makes libc++'s `_Atomic(T)` directly use an internal alias template to eliminate such divergence.

Fixes #<!-- -->168579.

---
Full diff: https://github.com/llvm/llvm-project/pull/168679.diff


2 Files Affected:

- (modified) libcxx/include/stdatomic.h (+6-1) 
- (added) libcxx/test/std/atomics/stdatomic.h.syn/alias.template.verify.cpp (+25) 


``````````diff
diff --git a/libcxx/include/stdatomic.h b/libcxx/include/stdatomic.h
index 2991030eee456..f27c8936ce790 100644
--- a/libcxx/include/stdatomic.h
+++ b/libcxx/include/stdatomic.h
@@ -135,7 +135,12 @@ using std::atomic_signal_fence                         // see below
 #      undef _Atomic
 #    endif
 
-#    define _Atomic(_Tp) ::std::atomic<_Tp>
+_LIBCPP_BEGIN_NAMESPACE_STD
+template <class _Tp>
+using __libcpp_atomic_alias _LIBCPP_NODEBUG = atomic<_Tp>;
+_LIBCPP_END_NAMESPACE_STD
+
+#    define _Atomic(_Tp) ::std::__libcpp_atomic_alias<_Tp>
 
 using std::memory_order _LIBCPP_USING_IF_EXISTS;
 using std::memory_order_relaxed _LIBCPP_USING_IF_EXISTS;
diff --git a/libcxx/test/std/atomics/stdatomic.h.syn/alias.template.verify.cpp b/libcxx/test/std/atomics/stdatomic.h.syn/alias.template.verify.cpp
new file mode 100644
index 0000000000000..34b542d540f6e
--- /dev/null
+++ b/libcxx/test/std/atomics/stdatomic.h.syn/alias.template.verify.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++23
+// UNSUPPORTED: no-threads
+
+// <stdatomic.h>
+
+// template<class T>
+//   using std-atomic = std::atomic<T>;        // exposition only
+//
+// #define _Atomic(T) std-atomic<T>
+
+// Verify that _Atomic(T) directly uses an alias template but not the std::atomic class template.
+// See also https://llvm.org/PR168579.
+
+#include <stdatomic.h>
+
+struct _Atomic(int) x;
+// expected-error-re at -1{{error: alias template '{{.*}}' cannot be referenced with the 'struct' specifier}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/168679


More information about the libcxx-commits mailing list