在编译时确定结构成员的字节偏移量? [英] Determining struct member byte-offsets at compile-time?

查看:104
本文介绍了在编译时确定结构成员的字节偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译时找到struct成员的字节偏移量.例如:

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;
}

我想知道normal的字节偏移是(在这种情况下,它应该是12.)

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

我知道我可以使用offsetof,但这是一个运行时函数,我希望不要使用它.

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?

编辑:offsetof在编译时,我很糟糕!

EDIT: offsetof is compile-time, my bad!

推荐答案

offsetof是编译时常量,如果我们查看草稿的C ++标准部分C.3 C标准库段落 2 说:

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

C ++标准库提供了来自C库的57个标准宏,如表149所示.

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

,并且该表包含offsetof.如果转到C99草案标准部分的7.17 通用定义段落,则 3 包括:

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

offsetof(类型,成员标识符)

offsetof(type, member-designator)

扩展为一个整数常量表达式,其类型为size_t,即 这是以字节为单位的偏移量[...]

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

这篇关于在编译时确定结构成员的字节偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆