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

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


https://github.com/frederick-vs-ja created https://github.com/llvm/llvm-project/pull/168679

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.

>From 2cacb78109dd186e847c11592531f9fbc4e9b6c3 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Wed, 19 Nov 2025 15:24:59 +0800
Subject: [PATCH] [libc++] Make `_Atomic(T)` directly use an alias template

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.
---
 libcxx/include/stdatomic.h                    |  7 +++++-
 .../stdatomic.h.syn/alias.template.verify.cpp | 25 +++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 libcxx/test/std/atomics/stdatomic.h.syn/alias.template.verify.cpp

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}}



More information about the libcxx-commits mailing list