[libcxx-commits] [libcxx] [libc++][NFC] Simplify the implementation of aligned_union (PR #185449)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Mar 9 09:17:46 PDT 2026


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/185449

None

>From db8e3ef80d8ece6925980c669fd0fe3a5f7ee431 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 9 Mar 2026 17:17:22 +0100
Subject: [PATCH] [libc++][NFC] Simplify the implementation of aligned_union

---
 libcxx/include/__type_traits/aligned_union.h | 22 +++++++++-----------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/libcxx/include/__type_traits/aligned_union.h b/libcxx/include/__type_traits/aligned_union.h
index 1223dc25e40a0..161ec73d106d9 100644
--- a/libcxx/include/__type_traits/aligned_union.h
+++ b/libcxx/include/__type_traits/aligned_union.h
@@ -19,24 +19,22 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <size_t _I0, size_t... _In>
-struct __static_max;
-
-template <size_t _I0>
-struct __static_max<_I0> {
-  static const size_t value = _I0;
+template <class _Arg0, class... _Args>
+union __union {
+  _ALIGNAS(_LIBCPP_PREFERRED_ALIGNOF(_Arg0)) _Arg0 __arg;
+  __union<_Args...> __u;
 };
 
-template <size_t _I0, size_t _I1, size_t... _In>
-struct __static_max<_I0, _I1, _In...> {
-  static const size_t value = _I0 >= _I1 ? __static_max<_I0, _In...>::value : __static_max<_I1, _In...>::value;
+template <class _Arg>
+union __union<_Arg> {
+  _Arg __arg;
 };
 
 template <size_t _Len, class _Type0, class... _Types>
 struct _LIBCPP_DEPRECATED_IN_CXX23 _LIBCPP_NO_SPECIALIZATIONS aligned_union {
-  static const size_t alignment_value =
-      __static_max<_LIBCPP_PREFERRED_ALIGNOF(_Type0), _LIBCPP_PREFERRED_ALIGNOF(_Types)...>::value;
-  static const size_t __len = __static_max<_Len, sizeof(_Type0), sizeof(_Types)...>::value;
+  using _Union                        = __union<char[_Len], _Type0, _Types...>;
+  static const size_t alignment_value = _LIBCPP_ALIGNOF(_Union);
+  static const size_t __len           = sizeof(_Union);
   typedef typename aligned_storage<__len, alignment_value>::type type;
 };
 



More information about the libcxx-commits mailing list