I have the following (simplified) code:
template <typename T>
class Crtp1 {};
template <typename T>
class Crtp2 {};
class C : public Crtp1<C>
, public Crtp2<C>
{
double d{};
bool b{};
};
static_assert(sizeof(double) == 8);
static_assert(sizeof(C) == 16);
As you can see, I have 2 empty base classes that use CRTP in order to add functions to this class. (Omitted in this example) As far as I'm aware, there is something called an empty base class optimization, which makes me expect that there ain't a memory impact of these bases.
If you compile the code with clang on Linux, this nicely does as expected, see the compiler-explorer as the static assert is accepted. However, the exact same code with MSVC 2019 takes 24 bytes instead of 16 bytes.
My question, preferably using C++17, however, I also accept C++20 answers, including the use of [[no_unique_address]]:
- How can I force empty base optimization on this example?