I have a structure that looks like this:
struct UploadConfig {
private:
const void * m_data; // 4 bytes (32bit)
glm::u16vec4 m_size; // 12 bytes (uint16 * 4 = +8)
uint8 m_mipmapsCount;
TextureDim::Type m_depthType;
TextureInternalFormat::Type m_internalFormat;
TextureUploadFormat::Type m_uploadFormat; // 16 bytes
TextureUploadComp::Type m_uploadCompType;
uint8 m_swizzleR :3;
uint8 m_swizzleG :3;
uint8 m_swizzleB :3;
uint8 m_swizzleA :3;
bool m_minFilter :1;
bool m_minmipFilterUsed :1;
bool m_minmipFilter :1;
bool m_maxFilter :1;
bool m_edgeClampX :1;
bool m_edgeMirrorX :1;
bool m_edgeClampY :1;
bool m_edgeMirrorY :1;
bool m_edgeClampZ :1;
bool m_edgeMirrorZ :1; // 20 bytes probably
};
const int iogwhgakfj = sizeof UploadConfig; // this is reported as 16 (???)
Where the enums are defined as uint8
like this:
struct TextureDim {
enum Type : uint8 {
dim2D,
dim3D,
dimCubic
};
};
But the size of this structure is really odd to me at 16 bytes, I expected it to be larger at 20 or even 24. Is the compiler turning my enums into bitfields behind my back? I mean... nice, but also seems weird it would do that with an enum type, and not a series of boolean. (Without the bitfields specified, this structure's size is 28)
edit
I tried adding some curveballs to confuse the compiler, but it still reports a size of 16 in the IDE (hovering over the value iogwhgakfj
)
My pitches were:
void UploadConfig::setMinFilter() {
// there's no way the compiler can predict this
m_minFilter = (uintptr(&m_internalFormat) & 7) == (uintptr(&m_uploadCompType) & 7);
}
Curiously too, it refuses to compute offsetof(TextureUploadConfig, m_uploadCompType);
in the IDE, but will do this for the values of m_mipmapsCount
and the members that appear prior to it.
Conclusion
It was because the enum wasn't defined yet. Define your enum types before you use them, because Visual Studio will very nicely highlight the syntax as if it were. Also maybe don't mess your code up so bad that you can't compile for a week straight.