具有可变数量参数的C ++宏 [英] C++ macro with variable number of arguments
本文介绍了具有可变数量参数的C ++宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要宏,它将在包含它的参数的数组中展开。例如:
I need macro that will expand in a array that contains it's arguments. For example:
#define foo(X0) char* array[1] = {X0}
#define foo(X0, X1) char* array[2] = {X0, X1}
我的问题是,我需要使用foo变量的参数,所以我想要能够使用foo(foo0),但也使用foo(foo0,foo1,foo2... fooN)。我知道它可能有:
and so on. My problem is that I need to use foo with variable number of arguments, so I want to be able to use foo("foo0") but also to use foo("foo0", "foo1", "foo2"..."fooN"). I know it's possible to have:
#define foo(...)
#define foo_1(X0) ..
#define foo_2(X0, X1) ..
#define foo_3(X0, X1, X2) ..
#define foo_N(X0, X1, ... XN) ..
并使用 __ VA_ARGS __ ,但我不知道如何扩展foo在foo_k宏取决于它的参数计数?这是可能吗?
And use __VA_ARGS__, but I don't know how may I expand foo in foo_k macro depending on it's parameter count? Is this possible?
推荐答案
如何:
#define FOO( ... ) char* x[] = { __VA_ARGS__ };
这篇关于具有可变数量参数的C ++宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文