I am writing a math vector and matrix library for practice, and I use a crtp base class in order to not have to define several functions more than once. I also inherit a static array class. Depending on the order of the inherited classes however I get a different size for my structure in msvc. Is this a bug, or is this something I should have expected? You can find a live demo at: https://godbolt.org/z/3EPVI5 Note that the same compiles just fine in gcc. The code for reference:
template<typename D>
struct crtp
{
};
template<typename T, int s>
struct arr_impl
{
T e[s];
};
template<typename T, int s>
struct vc : public arr_impl<T, s>, public crtp<vc<T, s>>
{
};
template<typename T, int s>
struct vc2 : public crtp<vc2<T, s>>, public arr_impl<T, s>
{
};
int main()
{
static_assert(sizeof(vc<vc<float,3>,2>) == 24);
static_assert(sizeof(vc2<vc2<float,3>,2>) == 24);
return 0;
}
I've narrowed it further, see: https://godbolt.org/z/tGCn_J As it seems only the nesting is required and an empty class:
struct empty_struct{};
template<typename T>
struct st
{
T a;
};
template<typename T>
struct vc : public empty_struct, public st<T> {};
template<typename T>
struct vc2 : public st<T>, public empty_struct{};
int main()
{
static_assert(sizeof(vc<vc<float>>) == 4);
static_assert(sizeof(vc2<vc2<float>>) == 4);
return 0;
}