我在网络上找不到答案的一个简单问题。在可变参数宏中,如何查找参数的数量?如果使用boost预处理器有解决方案,那就没问题了。
如果这有所区别,我正在尝试将不定数目的宏参数转换为boost预处理器序列、列表或数组进行进一步处理。
我在网络上找不到答案的一个简单问题。在可变参数宏中,如何查找参数的数量?如果使用boost预处理器有解决方案,那就没问题了。
如果这有所区别,我正在尝试将不定数目的宏参数转换为boost预处理器序列、列表或数组进行进一步处理。
BOOST_PP_VARIADIC_SIZE(...)
,它可以处理最多64个参数。__VA_OPT__
或编译器扩展的 ##__VA_ARGS__
来修复它,删除先前的逗号,例如:https://godbolt.org/z/X7OvnK - Justin你可以将字符串化并计算令牌:
int countArgs(char *args)
{
int result = 0;
int i = 0;
while (isspace(args[i]))
++i;
if (args[i])
++result;
while (args[i++])
{
if (args[i] == ',')
++result;
else if (args[i] == '\'')
i += 2;
else if (args[i] == '\"')
while (args[i++])
if (args[i + 1] == '\"' && args[i] != '\\')
break;
}
return result;
}
#define MACRO(...) \
{ \
int count = countArgs(#__VA_ARGS__); \
printf("NUM ARGS: %d\n",count); \
}
__typeof__
来使其在某些编译器上工作。 - Christoph