I'm experiencing an - at least to me - strange behavior when applying the sizeof
operator to a template specialization.
Consider these declarations:
#pragma pack(push, 1)
template <bool enable>
struct RGB_data {
typedef unsigned char typeRGB;
typeRGB R;
typeRGB G;
typeRGB B;
};
template <> struct RGB_data<false> {};
template <bool enable>
struct XYZ_data {
typedef float type3D;
type3D X;
type3D Y;
type3D Z;
};
template<> struct XYZ_data<false> {};
template <bool enable>
struct Alpha_data {
typedef unsigned char typeAlpha;
typeAlpha A;
};
template<> struct Alpha_data<false> {};
template <bool enable>
struct Confidence_data {
typedef unsigned char typeConfidence;
typeConfidence C;
};
template<> struct Confidence_data<false> {};
template <
bool enableXYZ,
bool enableRGB,
bool enableC,
bool enableA
>
struct Pixel :
public XYZ_data<enableXYZ>,
public RGB_data<enableRGB>,
public Alpha_data<enableA>,
public Confidence_data<enableC>
{};
typedef Pixel<true,false,false,false> Pixel3D;
typedef Pixel<true,false,true,false> Pixel3Dc;
#pragma pack(pop)
However, the size of the Pixel3Dc
struct is 14 bytes; the size of Pixel3D
is 14 bytes as well. The sizes of the data types in use are 1 and 4 for unsigned char
and float
, respectively.
Where are the additional bytes, and how can I achieve a struct size that matches with the size of the contained data?
Environment is VS 9.0, Windows7 x64, Compiled to 32Bit, Debug & Release mode.