问题描述
我想在编译时找到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:
,并且该表包含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:
这篇关于在编译时确定结构成员的字节偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!