From 96054f4ba22774a221fc218cb3cf9e8114e0a2b2 Mon Sep 17 00:00:00 2001 From: get <45425365+Minty-Meeo@users.noreply.github.com> Date: Mon, 12 Jun 2023 03:55:42 -0500 Subject: [PATCH] Give basic_memory_buffer allocator [[no_unique_address]] This allows stateless allocators to take up no space while still avoiding the empty base class optimization. --- include/fmt/core.h | 13 +++++++++++++ include/fmt/format.h | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 4961310a311f1..ca1518a5252d8 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -87,6 +87,9 @@ #define FMT_HAS_CPP17_ATTRIBUTE(attribute) \ (FMT_CPLUSPLUS >= 201703L && FMT_HAS_CPP_ATTRIBUTE(attribute)) +#define FMT_HAS_CPP20_ATTRIBUTE(attribute) \ + (FMT_CPLUSPLUS >= 202002L && FMT_HAS_CPP_ATTRIBUTE(attribute)) + // Check if relaxed C++14 constexpr is supported. // GCC doesn't allow throw in constexpr until version 6 (bug 67371). #ifndef FMT_USE_CONSTEXPR @@ -154,6 +157,16 @@ # endif #endif +#ifndef FMT_NO_UNIQUE_ADDRESS +# if FMT_HAS_CPP20_ATTRIBUTE(no_unique_address) +# define FMT_NO_UNIQUE_ADDRESS [[no_unique_address]] +# elif FMT_MSC_VERSION >= 1929 && FMT_CPLUSPLUS >= 202002L // VS2019 v16.10 and later +# define FMT_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] +# else +# define FMT_NO_UNIQUE_ADDRESS +# endif +#endif + #ifndef FMT_INLINE # if FMT_GCC_VERSION || FMT_CLANG_VERSION # define FMT_INLINE inline __attribute__((always_inline)) diff --git a/include/fmt/format.h b/include/fmt/format.h index 67d9caffde648..a8e8a40fa060b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -920,7 +920,7 @@ class basic_memory_buffer final : public detail::buffer { T store_[SIZE]; // Don't inherit from Allocator avoid generating type_info for it. - Allocator alloc_; + FMT_NO_UNIQUE_ADDRESS Allocator alloc_; // Deallocate memory allocated by the buffer. FMT_CONSTEXPR20 void deallocate() {