Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to find the byte offset of a struct member at compile-time. For example:

struct vertex_t
{
    vec3_t position;
    vec3_t normal;
    vec2_t texcoord;
}

I would want to know that the byte offset to normal is (in this case it should be 12.)

I know that I could use offsetof, but that is a run-time function and I'd prefer not to use it.

Is what I'm trying to accomplish even possible?

EDIT: offsetof is compile-time, my bad!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
308 views
Welcome To Ask or Share your Answers For Others

1 Answer

offsetof is a compile time constant, if we look at the draft C++ standard section C.3 C standard library paragraph 2 says:

The C++ standard library provides 57 standard macros from the C library, as shown in Table 149.

and the table includes offsetof. If we go to the C99 draft standard section 7.17 Common definitions paragraph 3 includes:

offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the value of which is the offset in bytes [...]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...