我想做类似以下的事情:
F_BEGIN
F(f1) {some code}
F(f2) {some code}
...
F(fn) {some code}
F_END
Run Code Online (Sandbox Code Playgroud)
并让它产生以下
int f1() {some code}
int f2() {some code}
...
int fn() {some code}
int (*function_table)(void)[] = { f1, f2, ..., fn };
Run Code Online (Sandbox Code Playgroud)
功能本身很容易.我似乎无法做的是跟踪函数表的所有名称,直到最后.
Chr*_*odd 14
使用预处理器执行此操作的常规方法是在宏中定义将另一个宏作为参数的所有函数,然后使用其他宏来提取所需的内容.对于你的例子:
#define FUNCTION_TABLE(F) \
F(f1, { some code }) \
F(f2, { some code }) \
F(f3, { some code }) \
:
F(f99, { some code }) \
F(f100, { some code })
#define DEFINE_FUNCTIONS(NAME, CODE) int NAME() CODE
#define FUNCTION_NAME_LIST(NAME, CODE) NAME,
FUNCTION_TABLE(DEFINE_FUNCTIONS)
int (*function_table)(void)[] = { FUNCTION_TABLE(FUNCTION_NAME_LIST) };
Run Code Online (Sandbox Code Playgroud)
如果您有C99兼容编译器,则预处理器具有可变长度参数列表.P99有一个预处理器P99_FOR,可以像你想要的那样进行"代码展开".要贴近你的榜样
#define MYFUNC(DUMMY, FN, I) int FN(void) { return I; }
#define GENFUNCS(...) \
P99_FOR(, P99_NARG(__VA_ARGS__), P00_IGN, MYFUNC, __VA_ARGS__) \
int (*function_table)(void)[] = { __VA_ARGS__ }
GENFUNCS(toto, hui, gogo);
Run Code Online (Sandbox Code Playgroud)
将扩展到以下(未经测试)
int toto(void) { return 0; }
int hui(void) { return 1; }
int gogo(void) { return 2; }
int (*function_table)(void)[] = { toto, hui, gogo };
Run Code Online (Sandbox Code Playgroud)