怎样才能通过C preprocessor(CPP)的列表? [英] How can I generate a list via the C preprocessor (cpp)?

查看:90
本文介绍了怎样才能通过C preprocessor(CPP)的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我愿做类似如下:

F_BEGIN

F(f1) {some code}
F(f2) {some code}
...
F(fn) {some code}

F_END

和有它生成以下

int f1() {some code}
int f2() {some code}
...
int fn() {some code}

int (*function_table)(void)[] = { f1, f2, ..., fn };

函数本身很容易。我似乎无法做的是跟踪所有的名字,直到年底的一下function_table。

The functions themselves are easy. What I can't seem to do is to keep track of all of the names until the end for the function_table.

我看着这个问题并的this问题,但我无法得到任何为我工作。
任何想法?

I looked at this question and this question but I couldn't get anything to work for me. Any ideas?

推荐答案

在preprocessor这样做的一般方法是定义在另需宏作为一个参数宏的所有功能,然后再使用其他宏提取你想要什么。为了您的例子:

The normal way of doing this with the preprocessor is to define all the functions in a macro that takes another macro as an argument, and then use other macros to extract what you want. For your example:

#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) };

这篇关于怎样才能通过C preprocessor(CPP)的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆