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
@mcc @jrose
if you have assumptions in your C# code testing those in C++ at compile time with static_assert is the best way. however, you need to ensure that tests and assumptions keep in sync. not sure if you are already doing that. but compile time is better than test run time