gpt4 book ai didi

将 offsetof 宏的结果连接 (##) 到标识符?

转载 作者:太空宇宙 更新时间:2023-11-04 05:39:50 27 4
gpt4 key购买 nike

我一直在尝试使用宏连接 (##) 和 offsetof 宏来生成标识符,但没有成功:

CMain.c:55: warning: implicit declaration of function 'Color__builtin_offsetof'

struct Color_methods_TABLE
{
struct
{
int8 getColor;
}
methods;
};

#define METHOD_NAME1(clazz, method) clazz##method
#define METHOD_NAME(clazz, method) METHOD_NAME1(clazz, method)

#define tdef_name(clazz, method) METHOD_NAME(clazz, method)

int main()
{
tdef_name(Color, offsetof(struct Color_methods_TABLE, methods.getColor));

return 0;
}

输出应该是:

Color0

其中 0 是 offsetof 宏的结果。这可以做到吗?感谢您的宝贵时间。

最佳答案

你不能为所欲为。 offsetof(与 sizeof 一样)是在预处理之后,在编译过程中计算的。

查看源代码的预处理形式。使用 GCC,您可以使用 gcc -C -E CMain.c > CMain.i 获取它,然后使用编辑器(或寻呼机)查看 CMain.i 内部,例如少 CMain.i

关于将 offsetof 宏的结果连接 (##) 到标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118880/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com