I'm writing C++. I have a struct

struct ExampleStruct {
int32_t i;
bool a;
bool b;
}

I know I'm running on a modern CPU such as x86_64 or ARM64, and I know I'm using a modern compiler such as recent clang, recent gcc, or recent msvc.

The byte offsets of "a" and "b", relative to the start of ExampleStruct, are:

4 and 5
30.2%
4 and 8
16.3%
It depends on platform/compiler
44.1%
U gotta use a #pragma
9.4%
Poll ended at .
@mcc On the one hand, this struct’s layout is fully defined (on a per-platform basis). On the other hand, it worries me that you have a need to rely on this. On the third hand that I have grown for rhetorical purposes, https://en.cppreference.com/w/cpp/types/offsetof.html exists and even if you can’t use it wherever you actually need to know offsets, you can put it in a static_assert.
offsetof - cppreference.com

@jrose For some context:
- This particular project has a fixed, predictable set of target platforms and compilers it needs to work with.
- This particular project has automated tests, so if a platform we are running on produces surprising alignments, we'll know as soon as we run the automated test where I check that
- Although offsetof is a great suggestion, it doesn't help me here because I am doing C# FFI stuff and it is the C# code which needs to know the offsets

@jrose However, despite the automated tests, if someone had responded to my post with "uhh yo win32 has 3 bytes padding between each bool" it would have saved me a little bit of time.

someone else pointed there's no *downside* to just tossing #pragma pack in there, so i did.

@mcc god dammit windows

Sorry, I should have remembered the ABI is sufficiently different.

(but I maintain: put the offsetof in a static_assert.)

@jrose Sorry; I am describing a hypothetical in which the win32 ABI is different, not saying it is different.
@mcc @jrose it used to be different when c++ wasn't standardised. Win32 BOOL was 32bit. But these days, it is a byte aligned byte.
@fabs @mcc @jrose still platform dependent. some exotic platforms don't have 8-bit char/std::byte types
@PeterSommerlad @mcc @jrose yes, fortunately windows doesn't support them 😁