cha*_*m15 3 c c++ c-preprocessor
我想有一个函数打印出有关结构的成员变量的信息.为了使函数尽可能简单(并且没有错误),我也不想手动传入类型.这使我需要能够评估传递给我的宏的参数:
#ifndef preprocessor_stringify
#define preprocessor_stringify(s) #s
#endif
typedef struct test_s {
void (*ptr)(void*);
} test;
void doSomething_(char *name, int offset, int size){
printf("%s %d %d\n", name, offset, size);
}
#define doSomething(name, container) (\
doSomething_(\
preprocessor_stringify(name),\
offsetof(container, name),\
sizeof(container->name))\
);
int main(){
doSomething(ptr, test);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会产生编译错误
test.cpp:21:19: error: expected primary-expression before ‘->’ token
sizeof(container->name))\
有想法该怎么解决这个吗?我希望解决方案兼容c和c ++,理想情况下.
#include <stdio.h>
#include <stddef.h>
#ifndef preprocessor_stringify
#define preprocessor_stringify(s) #s
#endif
typedef struct test_s {
void (*ptr)(void*);
} test;
void doSomething_(char const *name, int offset, int size){
printf("%s %d %d\n", name, offset, size);
}
#define doSomething(name, container) (\
doSomething_(\
preprocessor_stringify(name),\
offsetof(container, name),\
sizeof(((container*)0)->name))\
);
int main(){
doSomething(ptr, test);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我做了两处改动:
const char[]
void doSomething_(char const *name, int offset, int size){
Run Code Online (Sandbox Code Playgroud)
sizeof(((container*)0)->name))\
Run Code Online (Sandbox Code Playgroud)
其中一条评论提到指针转换是丑陋的.我同意,让我们把它限制在一个我们可以重复使用的宏.
#define sizeof_member(Class, Member) sizeof ((Class*)0)->Member
#define doSomething(name, container) (\
doSomething_(\
preprocessor_stringify(name),\
offsetof(container, name),\
sizeof_member(container, name)) \
);
Run Code Online (Sandbox Code Playgroud)