当括号导致语法错误时,如何在宏参数内的大括号内添加逗号?
我定义了一些宏,使定义结构数组变得更简单,但我找不到一种在不生成错误的情况下使用它们的方法。以下是宏(以及一些示例结构,以演示为什么可以使用宏(我填充的实际结构稍微复杂一些)):
struct string_holder {
const char *string;
};
struct string_array_holder {
struct string_holder *holders;
};
#define DEFINE_STRING_ARRAY_HOLDER(name, values) \
static struct string_holder name##__array[] = values; \
static struct string_array_holder name = { name##__array }
#define WRAP_STRING(string) { string }
当您使用它来声明包含一项的数组时,它工作得很好:
DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, {
WRAP_STRING("my string")
});
但是当我使用多个项目时:
DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, {
WRAP_STRING("hello"),
WRAP_STRING("world")
});
我收到此错误:
错误:为类似函数的宏调用提供了太多参数
因此它将大括号中的逗号解释为参数分隔符。我遵循 这个问题 的建议,并在有问题的参数周围加上括号:
DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, ({
WRAP_STRING("hello"),
WRAP_STRING("world")
}));
现在,当我尝试编译它时,它会解释 ({ ... })
作为语句表达式 和抱怨:
警告:使用 GNU 语句表达式扩展
(由于将其解释为语句表达式而导致一堆语法错误)
错误:文件范围内不允许使用语句表达式
我怎样才能:
- 使用宏而不会出现错误(首选),或
- 重写宏以在这些情况下工作?
I've defined a few macros that make it simpler to define an array of structures, but I can't find a way to use them without generating errors. Here are the macros (and a few example structures to demonstrate why the macros might be used (the actual structures I'm populating are a little more complex)):
struct string_holder {
const char *string;
};
struct string_array_holder {
struct string_holder *holders;
};
#define DEFINE_STRING_ARRAY_HOLDER(name, values) \
static struct string_holder name##__array[] = values; \
static struct string_array_holder name = { name##__array }
#define WRAP_STRING(string) { string }
It works just fine when you use it to declare an array with one item:
DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, {
WRAP_STRING("my string")
});
But when I use multiple items:
DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, {
WRAP_STRING("hello"),
WRAP_STRING("world")
});
I get this error:
error: too many arguments provided to function-like macro invocation
So it's interpreting the comma in the braces as an argument separator. I follow the advice from this question and put parentheses around the problematic argument:
DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, ({
WRAP_STRING("hello"),
WRAP_STRING("world")
}));
Now when I try to compile it, it interprets ({ ... })
as a statement expression and complains:
warning: use of GNU statement expression extension
(a bunch of syntax errors resulting from its interpretation as a statement expression)
error: statement expression not allowed at file scope
How can I either:
- Use the macro without errors (preferred), or
- Rewrite the macro[s] to work in these circumstances?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Dmitri 是对的,可变参数宏是正确的选择。
我放置了一些示例代码,用于测试给定的键是否是值列表的成员:
注意
__VA_ARGS__
的用法。更新:
如果您不喜欢在任意位置使用
__VA_ARGS__
,一个粗略的解决方案是使用“解包器”宏:您可以像前缀运算符一样使用它。 ;-)
Dmitri is right, variadic macros are the way to go.
I put some example code I use to test if given key is member of a list of values:
Mind the usage of
__VA_ARGS__
.Update:
A crude solution if you don't like
__VA_ARGS__
at arbitrary places would be an "unwrapper" macro:You could use it like a prefix-operator. ;-)
是的,使用
__VA_ARGS__
,但是 Kay 的 解决方案太复杂了。只要做:就足够了。然后,您可以按照您的预期使用此宏:
Yes, use
__VA_ARGS__
, but Kay's solution is much too complicated. Just doing:suffices. You may then just use this macro as you intended with: