Such a pragma would be permitted by the language standard, but I'm not aware of any compiler that implements such a thing.
In C, the behavior of #pragma
is specified in section 6.10.6 of the standard (the link is to the latest draft):
A preprocessing directive of the form
# pragma pp-tokensopt new-line
where the preprocessing token STDC
does not immediately
follow pragma
in the directive (prior to any macro replacement)
causes the implementation to behave in an implementation-defined
manner. The behavior might cause translation to fail or cause the
translator or the resulting program to behave in a non-conforming
manner. Any such pragma that is not recognized by the implementation
is ignored.
So a #pragma
can, in effect, violate the rules of the language.
The relevant rule in this case is that struct members are laid out in the order in which they're declared. 6.7.2.1 paragraph 15:
Within a structure object, the non-bit-field members and the units in
which bit-fields reside have addresses that increase in the order in
which they are declared. A pointer to a structure object, suitably
converted, points to its initial member (or if that member is a
bit-field, then to the unit in which it resides), and vice versa.
There may be unnamed padding within a structure object, but not at its
beginning.
The bad news: The C standard requires struct members to be laid out in the order in which they're declared. The first member must be at offset 0. There may be arbitrary padding between members, or after the last one, but they cannot be reordered.
The good news: The language permits an implementation to define a #pragma
that specifies a layout that violates the above rule.
The bad news: As far as I know, no implementation actually does so. Even if one did, there are other implementations that do not, so any code that uses such a #pragma
would be non-portable. (Though at least if the name of the #pragma
is unique, any compilers that don't recognize it are required to ignore it, so your code would still compile.)
That's for C. The C++ rules for #pragma
are very similar to the C rules. I'm reasonably sure the C++ rules for struct layout are also similar to C's; inheritance makes things a little more complex.