[libcxx-commits] [libcxx] [libc++] Make std::allocator always trivially default constructible (PR #169914)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Nov 28 05:05:12 PST 2025
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/169914
This is technically ABI breaking, since `is_trivial` and `is_trivially_default_constructible` now return different results. However, I don' think that's a significant issue, since `allocator` is almost always used in classes which own memory, making them non-trivial anyways.
>From 833941969fe948739a7dcec487154be49e9c9e3c Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Fri, 28 Nov 2025 14:03:19 +0100
Subject: [PATCH] [libc++] Make std::allocator always trivially default
constructible
---
libcxx/include/__memory/allocator.h | 30 +----------------------------
1 file changed, 1 insertion(+), 29 deletions(-)
diff --git a/libcxx/include/__memory/allocator.h b/libcxx/include/__memory/allocator.h
index 52f4122a9bf5f..827d37aae1854 100644
--- a/libcxx/include/__memory/allocator.h
+++ b/libcxx/include/__memory/allocator.h
@@ -14,14 +14,11 @@
#include <__cstddef/ptrdiff_t.h>
#include <__cstddef/size_t.h>
#include <__memory/addressof.h>
-#include <__memory/allocate_at_least.h>
#include <__memory/allocator_traits.h>
#include <__new/allocate.h>
#include <__new/exceptions.h>
#include <__type_traits/is_const.h>
#include <__type_traits/is_constant_evaluated.h>
-#include <__type_traits/is_same.h>
-#include <__type_traits/is_void.h>
#include <__type_traits/is_volatile.h>
#include <__utility/forward.h>
@@ -51,33 +48,8 @@ class allocator<void> {
};
#endif // _LIBCPP_STD_VER <= 17
-// This class provides a non-trivial default constructor to the class that derives from it
-// if the condition is satisfied.
-//
-// The second template parameter exists to allow giving a unique type to __non_trivial_if,
-// which makes it possible to avoid breaking the ABI when making this a base class of an
-// existing class. Without that, imagine we have classes D1 and D2, both of which used to
-// have no base classes, but which now derive from __non_trivial_if. The layout of a class
-// that inherits from both D1 and D2 will change because the two __non_trivial_if base
-// classes are not allowed to share the same address.
-//
-// By making those __non_trivial_if base classes unique, we work around this problem and
-// it is safe to start deriving from __non_trivial_if in existing classes.
-template <bool _Cond, class _Unique>
-struct __non_trivial_if {};
-
-template <class _Unique>
-struct __non_trivial_if<true, _Unique> {
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT {}
-};
-
-// allocator
-//
-// Note: For ABI compatibility between C++20 and previous standards, we make
-// allocator<void> trivial in C++20.
-
template <class _Tp>
-class allocator : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > {
+class allocator {
static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
More information about the libcxx-commits
mailing list