does offsetof work with typeof if type is new?

143 views Asked by At

I read on cppreference (https://en.cppreference.com/w/c/types/offsetof):

Even though it is specified in C23 that defining a new type in offsetof is undefined behavior, such usage is only partially supported by implementations even in earlier modes: offsetof(struct Foo { int a; }, a) is usually supported, but offsetof(struct Foo { int a, b; }, a) is not because of the comma in the definition of struct Foo.

So it won't work with new types because of the commas in the definitions, does that mean using typeof will make it work (since it'll wrap the type in parentheses)?

offsetof(typeof(struct Foo { int a,b; }), a);

edit:

if this doesn't work, is there a workaround to have the struct be defined inside the offsetof?

for example:

void *dummy;
offsetof(typeof(*((struct { int a; int b; }*)dummy)), a);
1

There are 1 answers

2
Eric Postpischil On BEST ANSWER

C 2023 draft N3096 7.21 4 says, of offsetof(type, member-designator):

… If the specified type defines a new type or if the specified member is a bit-field, the behavior is undefined.

Therefore the C standard does not require offsetof to work with a new type in a conforming C implementation, regardless of whether or not the macro argument for type contains a comma. If the argument contains a typeof in which a new type is defined, the argument defines a new type and is subject to the above.

Another reason it might not work with a new type, hypothetically, is the macro might use the type argument in its replacement list in a way that causes problems due to multiple definitions of the type. It is not a correct logical inference that because X (presence of a comma) causes Y (offsetof will not work) that Not X will cause Not Y.