From 393f024a4d6fc8c9949f0c05b1be66d16cf5ea00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Dee=20=28J=C5=8Dshin=29?= Date: Sat, 15 Jun 2024 18:53:56 -0700 Subject: [PATCH] final classes can't be empty-base-optimized For that we need [[no_unique_address]]. Actually wait, I think maybe we have [[no_unique_address]]! Maybe we can get rid of compressed_pair entirely... --- ctl/compressed_pair.h | 5 +++-- test/ctl/unique_ptr_test.cc | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ctl/compressed_pair.h b/ctl/compressed_pair.h index c9ecfde15..9c1449864 100644 --- a/ctl/compressed_pair.h +++ b/ctl/compressed_pair.h @@ -3,6 +3,7 @@ #ifndef COSMOPOLITAN_CTL_COMPRESSED_PAIR_H_ #define COSMOPOLITAN_CTL_COMPRESSED_PAIR_H_ #include <__type_traits/is_empty.h> +#include <__type_traits/is_final.h> #include <__utility/forward.h> #include <__utility/move.h> #include <__utility/swap.h> @@ -12,7 +13,7 @@ namespace ctl { namespace __ { template -concept Empty = std::is_empty_v; +concept EmptyBase = std::is_empty_v && !std::is_final_v; template struct pair_elem @@ -37,7 +38,7 @@ struct pair_elem T t; }; -template +template struct pair_elem : private T { using value_type = T; diff --git a/test/ctl/unique_ptr_test.cc b/test/ctl/unique_ptr_test.cc index baa283a27..d786e3479 100644 --- a/test/ctl/unique_ptr_test.cc +++ b/test/ctl/unique_ptr_test.cc @@ -54,6 +54,21 @@ struct SetsGDeleter } }; +struct FinalDeleter final +{ + void operator()(auto*) const noexcept + { + } +}; + +struct StatefulDeleter +{ + char state; + void operator()(auto*) const noexcept + { + } +}; + static_assert(sizeof(Ptr) == sizeof(int*)); struct SetsGCtor @@ -185,6 +200,13 @@ main() } #endif + { + int a; + // Should compile. + Ptr x(&a); + Ptr y(&a); + } + // next is 18 return 0;