I have a class hierarchy that is using CRTP for "inheritance" across template specializations. This class represents a math matrix. When I instantiate this concrete class, sizeof(Matrix<int, 4, 4>)
returns 4 more bytes than I'm expecting. Normally I'd attribute that extra memory to a vtable somewhere, but in that case I'd expect it to be 8 bytes (pointer), and I'd only expect that if I had any virtual functions, but I do not. On top of that, when I print out std::is_polymorphic<Matrix<float, 4, 4>>::value
the result is false. It probably isn't an alignment packing issue because that should come out to a nice perfect 2^6. I'm either missing something key about the c++ standard, or the win32 c++ compiler is doing something fishy.
The hierarchy looks like so, (omitting non properties for brevity):
template<class T, uint32_t length, class CRTP>
struct BaseDimensional {
// only functions, no virtuals
}
template<class T, uint32_t length, class CRTP>
struct IntegralNumberDimensional {
// only functions, no virtuals
};
template<class T, uint32_t row, uint32_t col, class CRTP>
class BaseMatrix : public BaseDimensional<T, row * col, CRTP> {
protected:
union {
T mat[col][row]{};
T arr[col * row];
};
public:
// functions, no virtuals
}
template<class T, uint32_t row, uint32_t col>
struct Matrix : public BaseMatrix<T, row, col, Matrix<T, row, col>>,
public IntegralNumberDimensional<T, row * col, Matrix<T, row, col>> {
// empty
}
I am using the compiler built into visual studio 2019, with flags /SUBSYSTEM:windows /ENTRY:mainCRTStartup /std:c++17 /EHsc /MDd /Zi
if that matters. When printing out all the bytes in the object the extra bytes appear at the beginning. When debugging, the only values shown are the union. sizeof(Matrix<int, 4, 4>)
comes out to 68. Not finding any other documentation that suggests this behavior. If anyone knows or has a suggestion to dig into this further, I'd greatly appreciate it.