C++中__builtin_offsetof
运算符(或Symbian 中的运算符)的用途是什么?_FOFF
此外它还返回什么?指针?字节数?
它是 GCC 编译器提供的内置函数,用于实现offsetof
C 和 C++ 标准指定的宏:
它返回 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
值)。
你什么时候可以使用它?
可能相关的一种情况是表驱动操作,用于从文件中读取大量不同的配置参数并将值填充到同样庞大的数据结构中。将巨大减少到如此微不足道(并忽略各种必要的现实世界实践,例如在标题中定义结构类型),我的意思是一些参数可能是整数和其他字符串,并且代码可能看起来像:
#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
运算符的目的是编译器供应商可以继续 #defineoffsetof()
宏,但让它与定义 unary 的类一起使用operator&
。典型的 C 宏定义offsetof()
仅在(&lvalue)
返回该右值的地址时才起作用。IE
#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
正如@litb 所说:结构/类成员的字节偏移量。在 C++ 中,有些情况下它是未定义的,以防编译器抱怨。IIRC,实现它的一种方法(至少在 C 中)是
#define offsetof(type, member) (int)(&((type *)0)->member)
但我确信这有问题,但我会把它留给感兴趣的读者指出......