Skip to content

Commit 4ce432e

Browse files
author
SSS-XPS-9530
committed
Make sizeof(Holder<void>) == sizeof(EmptyHolder)
1 parent bcd340a commit 4ce432e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

include/any_shared_ptr.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,22 @@ namespace xxx {
375375

376376
struct EmptyHolder final : public IHolder
377377
{
378+
// Not used - is required so that sizeof(Holder<void>) == sizeof(EmptyHolder)
379+
std::shared_ptr<void> my_ptr;
380+
378381
EmptyHolder() = default;
379382

380383
bool has_value() const noexcept { return false; }
381384
const std::type_info& type() const noexcept { return typeid(void); }
382385
bool unique() const noexcept final { return false; }
383386
const IHolder * clone(void* const inplaceMemory) const noexcept final { return ::new (inplaceMemory) EmptyHolder(); }
384387
void throw_held_pointer() const final {}
385-
std::shared_ptr<void> make_shared_ptr_alias(void* ) const noexcept final { return std::shared_ptr<void>(); }
388+
std::shared_ptr<void> make_shared_ptr_alias(void* ) const noexcept final { return my_ptr; }
386389
};
387390

391+
static_assert(sizeof(Holder<void>) == sizeof(EmptyHolder), "It's not essential but sizeof(Holder<void>) == sizeof(EmptyHolder)"
392+
" is better so that all bytes in my_inplace_storage are initialized");
393+
388394
using storage_t = typename std::aligned_storage<sizeof(Holder<void>), std::alignment_of<Holder<void>>::value>::type;
389395

390396
// Inplace storage to hold Holder<T>

0 commit comments

Comments
 (0)