__builtin_offsetof
操作符 (或 Symbian 中的 _FOFF
操作符) 在 C++ 中的目的是什么?
此外,它返回什么?指针还是字节数?
它是GCC编译器提供的内置函数,用于实现由C和C++标准指定的offsetof
宏:
它返回POD结构体/联合体成员所在的字节偏移量。
示例:
struct abc1 { int a, b, c; };
union abc2 { int a, b, c; };
struct abc3 { abc3() { } int a, b, c; }; // non-POD
union abc4 { abc4() { } int a, b, c; }; // non-POD
assert(offsetof(abc1, a) == 0); // always, because there's no padding before a.
assert(offsetof(abc1, b) == 4); // here, on my system
assert(offsetof(abc2, a) == offsetof(abc2, b)); // (members overlap)
assert(offsetof(abc3, c) == 8); // undefined behavior. GCC outputs warnings
assert(offsetof(abc4, a) == 0); // undefined behavior. GCC outputs warnings
@Jonathan提供了一个很好的例子,你可以在这里使用它。我记得曾经看到它被用于实现侵入式列表(其中数据项本身包括next和prev指针),但是可惜我不记得它在实现中有什么帮助了。
正如@litb指出并且@JesperE所展示的,offsetof()
提供了一个以字节为单位的整数偏移量(作为size_t
值)。
什么时候会使用它?
其中一个可能相关的情况是表驱动操作,用于从文件中读取大量不同配置参数并将值塞入同样巨大的数据结构中。将“巨大”减少到 SO 级别(忽略一系列必要的现实实践,例如在头文件中定义结构类型),我的意思是有些参数可能是整数而其他参数则可能是字符串,代码可能看起来像这样:
#include <stddef.h>
typedef stuct config_info config_info;
struct config_info
{
int parameter1;
int parameter2;
int parameter3;
char *string1;
char *string2;
char *string3;
int parameter4;
} main_configuration;
typedef struct config_desc config_desc;
static const struct config_desc
{
char *name;
enum paramtype { PT_INT, PT_STR } type;
size_t offset;
int min_val;
int max_val;
int max_len;
} desc_configuration[] =
{
{ "GIZMOTRON_RATING", PT_INT, offsetof(config_info, parameter1), 0, 100, 0 },
{ "NECROSIS_FACTOR", PT_INT, offsetof(config_info, parameter2), -20, +20, 0 },
{ "GILLYWEED_LEAVES", PT_INT, offsetof(config_info, parameter3), 1, 3, 0 },
{ "INFLATION_FACTOR", PT_INT, offsetof(config_info, parameter4), 1000, 10000, 0 },
{ "EXTRA_CONFIG", PT_STR, offsetof(config_info, string1), 0, 0, 64 },
{ "USER_NAME", PT_STR, offsetof(config_info, string2), 0, 0, 16 },
{ "GIZMOTRON_LABEL", PT_STR, offsetof(config_info, string3), 0, 0, 32 },
};
现在可以编写一个通用函数,从配置文件中读取行,忽略注释和空行。然后它会隔离参数名称,在desc_configuration
表格中查找(你可能需要对其进行排序以进行二分搜索 - 多个SO问题都涉及到了这个)。当它找到正确的config_desc
记录时,它可以将找到的值和config_desc
条目传递给两个例程之一 - 用于处理字符串的例程或用于处理整数的例程。
其中重要的部分是:
static int validate_set_int_config(const config_desc *desc, char *value)
{
int *data = (int *)((char *)&main_configuration + desc->offset);
...
*data = atoi(value);
...
}
static int validate_set_str_config(const config_desc *desc, char *value)
{
char **data = (char **)((char *)&main_configuration + desc->offset);
...
*data = strdup(value);
...
}
这样可以避免为结构中的每个成员编写单独的函数。
__offsetof
运算符的目的是,编译器供应商可以继续定义offsetof()
宏,并使其适用于定义了一元operator&
的类。典型的C宏定义offsetof()
仅在(&lvalue)
返回该rvalue的地址时起作用。即:#define offsetof(type, member) (int)(&((type *)0)->member) // C definition, not C++
struct CFoo {
struct Evil {
int operator&() { return 42; }
};
Evil foo;
};
ptrdiff_t t = offsetof(CFoo, foo); // Would call Evil::operator& and return 42
&
返回一个指针,通常不会将其作为参数。请看这个例子,&
应用于 foo
,它是一个对象。 - MSaltersCFoo
不同,CFoo*
是一种内置类型(即指针)。此外,即使在C++中,语法规则(请查看您的书)也会忽略重载。运算符优先级是固定的。这意味着为了C兼容性,“&foo->bar”必须在有和没有运算符重载的情况下解析相同。 - MSalters如@litb所说:结构体/类成员的偏移量(以字节为单位)。在C ++中,有些情况下它是未定义的,在这种情况下编译器会发出警告。我记得,在C中实现它的一种方法(至少是)是执行
#define offsetof(type, member) (int)(&((type *)0)->member)