我想用D / mixin和C / C++预处理程序做同样的事情。我想编写一个函数来生成参数列表。例如:

#define MK_FN_FOO(n,t) …

MK_FN_FOO(3,float)

/* it will expand into */
void foo(float x0, float x1, float x2) {
    /* do something else here */
}

我有个主意,但遇到了问题。我必须进行递归,而且我不知道该怎么做:
#define MK_FOO(n,t) void foo(MK_FOO_PLIST(n-1,t)) { }
#define MK_FOO_PLIST(n,t) t xn, MK_FOO_PLIST(n-1,t) /* how stop that?! */

最佳答案

Boost库具有大量扩展的元编程以及其他所有预处理器库。可以使用它们的实用程序预处理程序指令来执行这种操作,这将比您自己做起来容易得多,尽管仍然有些令人困惑:)

我建议您从这里开始:

http://www.boost.org/doc/libs/1_53_0/libs/preprocessor/doc/index.html

http://www.boost.org/doc/libs/?view=category_Preprocessor

编辑:这是关于它们的另一个教程:
Boost.Preprocessor - Tutorial

关于c++ - 通过宏进行元编程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16543280/

10-09 08:36
查看更多