gpt4 book ai didi

c++ - 通过宏进行元编程

转载 作者:搜寻专家 更新时间:2023-10-31 01:09:53 34 4
gpt4 key购买 nike

我想用 C/C++ 预处理器做一些我们可以用 D 的混入做的事情。我想写一个函数来生成参数列表。例如:

#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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com