The only reason padding is ever added to structures and classes is to satisfy the alignment requirements of its members. Note that the alignment of a structure is the alignment of its member with the maximum alignment requirement. The size of a structure is a multiple of its alignment, trailing padding is added to satisfy that.
Since the members of Vector3d
are of the same type the alignment requirements are already satisfied and, hence, there isn't any padding between the members or at the end of the structure.
There is no requirement in the C++ standard to insert arbitrary padding for no reason.
To be 100% sure, throw in a static_assert
:
static_assert(sizeof(Vector3d<float>) == 3 * sizeof(float), "Unexpected layout.");
static_assert(sizeof(Vector3d<double>) == 3 * sizeof(double), "Unexpected layout.");
Some people say that the guarantee is not explicitly spelled out in the C++ standard and, hence, one cannot rely on that. However, the C++ standard is often under-specified, so that one needs to understand the rationale behind it. In my personal opinion, those people spread fear, uncertainty and doubt for no good reason.