我想用 C/C++ 预处理器对 D 的 mixins 做同样的事情。我想编写一个函数来生成参数列表。例如:
#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/