[libcxx-commits] [libcxx] [libc++][NFC] Simplify the implementation of aligned_union (PR #185449)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jun 24 00:38:03 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
Instead of manually calculating the size and alignment of a union, we can just generate an actual union and take the size and alignment of that.
---
Full diff: https://github.com/llvm/llvm-project/pull/185449.diff
1 Files Affected:
- (modified) libcxx/include/__type_traits/aligned_union.h (+10-12)
``````````diff
diff --git a/libcxx/include/__type_traits/aligned_union.h b/libcxx/include/__type_traits/aligned_union.h
index 1223dc25e40a0..d8a789a82d58c 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> {
+ _ALIGNAS(_LIBCPP_PREFERRED_ALIGNOF(_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 _LIBCPP_NODEBUG = __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;
};
``````````
</details>
https://github.com/llvm/llvm-project/pull/185449
More information about the libcxx-commits
mailing list