数组初始化宏
我试图想出一个宏来做类似下面的事情,
MY_MACRO(type,name,args...)
MY_MACRO(int,_array,1,2,3)
并扩展到,
int _array[] = {1,2,3};
coll* __array = my_call(&array[0],&array[1],&array[2]);
有/没有编译器特定的魔法这可能吗?
my_call 需要可变数量的参数,我不想直接传递数组。
编辑:我已经使用以下 SO 问题中接受的答案来处理 var args,
这样我就可以找到数组中有多少个元素。
I am trying to come up with a macro to do something like the following,
MY_MACRO(type,name,args...)
MY_MACRO(int,_array,1,2,3)
and expand to,
int _array[] = {1,2,3};
coll* __array = my_call(&array[0],&array[1],&array[2]);
is this possible with/without compiler specific magic?
my_call expects variable number of arguments and I do not want to pass the array directly.
EDIT: I am already using the accepted answer from the following SO question for var args,
Macro returning the number of arguments it is given in C?
So I can find how many elements are in the array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从(C99,非 GCC 特定)开始:
my_call
部分会更困难;网上有一些宏可以计算参数的数量,但是您需要像 Boost.Preprocessor (应该在 C 中工作)这样的东西来将my_call
应用于数组的连续元素。最多有多少个元素?您可能想要类似的东西:等等。
You can start with (C99, not GCC-specific):
The
my_call
part will be more difficult; there are macros online that can count the number of arguments, but you will need something like Boost.Preprocessor (which should work in C) to applymy_call
to the consecutive elements of the array. How many elements do you have as a maximum? You might want something like:and so on.