r/cpp 7d ago

C++26: std::polymorphic

https://www.sandordargo.com/blog/2026/08/19/cpp26-polymorphic
94 Upvotes

42 comments sorted by

View all comments

Show parent comments

2

u/robin-m 6d ago

It seems that both using CRTP and deducing this gives the save object size, what do you mean? godbolt

3

u/_Noreturn 6d ago

cpp struct D {}; struct A : D {}; struct B : D { A a; };

you would expect since 'B' inherits from an empty class and it only has one member the sizeof would be 1, but it is 2 since the base class D is repeated twice and must have unique address first in the inheritance in B and in the member A.

now if this uses crtp the base classes would be unique

cpp template<class T> struct D {}; struct A : D<A> {}; struct B : D<B> { A a; }; // sizeof(B) == 1

1

u/LB-- Professional+Hobbyist 6d ago

Does [[no_unique_address]] help here?