当括号导致语法错误时,如何在宏参数内的大括号内添加逗号?

发布于 2025-01-03 11:26:24 字数 1608 浏览 5 评论 0原文

我定义了一些宏,使定义结构数组变得更简单,但我找不到一种在不生成错误的情况下使用它们的方法。以下是宏(以及一些示例结构,以演示为什么可以使用宏(我填充的实际结构稍微复杂一些)):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2025-01-10 11:26:24

Dmitri 是对的,可变参数宏是正确的选择。

我放置了一些示例代码,用于测试给定的键是否是值列表的成员:

#define _IN(KEY, ...)                                             \
({                                                                \
  typedef __typeof(KEY) _t;                                       \
  const _t _key = (KEY);                                          \
  const _t _values[] = { __VA_ARGS__ };                           \
  _Bool _r = 0;                                                   \
  unsigned int _i;                                                \
  for (_i = 0; _i < sizeof(_values) / sizeof(_values[0]); ++_i) { \
    if (_key == _values[_i]) {                                    \
      _r = 1;                                                     \
      break;                                                      \
    }                                                             \
  }                                                               \
  _r;                                                             \
})

注意 __VA_ARGS__ 的用法。

更新:
如果您不喜欢在任意位置使用 __VA_ARGS__ ,一个粗略的解决方案是使用“解包器”宏:

#define UNWRAP(...) __VA_ARGS__

您可以像前缀运算符一样使用它。 ;-)

#include <stdio.h>

/* "unwrapper": */
#define UNWRAP(...) __VA_ARGS__

/* your macros: */
#define WRAP(NAME, ELEMS) static const char *NAME[] = { UNWRAP ELEMS }

int main(void) {
  WRAP(some_test, ("a", "b", "c"));
  printf("The second elem in some_test is: '%s'\n", some_test[1]);
  return 0;
}

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:

#define _IN(KEY, ...)                                             \
({                                                                \
  typedef __typeof(KEY) _t;                                       \
  const _t _key = (KEY);                                          \
  const _t _values[] = { __VA_ARGS__ };                           \
  _Bool _r = 0;                                                   \
  unsigned int _i;                                                \
  for (_i = 0; _i < sizeof(_values) / sizeof(_values[0]); ++_i) { \
    if (_key == _values[_i]) {                                    \
      _r = 1;                                                     \
      break;                                                      \
    }                                                             \
  }                                                               \
  _r;                                                             \
})

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:

#define UNWRAP(...) __VA_ARGS__

You could use it like a prefix-operator. ;-)

#include <stdio.h>

/* "unwrapper": */
#define UNWRAP(...) __VA_ARGS__

/* your macros: */
#define WRAP(NAME, ELEMS) static const char *NAME[] = { UNWRAP ELEMS }

int main(void) {
  WRAP(some_test, ("a", "b", "c"));
  printf("The second elem in some_test is: '%s'\n", some_test[1]);
  return 0;
}
夏了南城 2025-01-10 11:26:24

是的,使用 __VA_ARGS__,但是 Kay解决方案太复杂了。只要做:

#define DEFINE_STRING_ARRAY_HOLDER(name, ...)                 \
    static struct string_holder name##_array[] = __VA_ARGS__; \
    static struct string_array_holder name = { name##_array }

就足够了。然后,您可以按照您的预期使用此宏:

DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, {
    WRAP_STRING("hello"),
    WRAP_STRING("world")
});

Yes, use __VA_ARGS__, but Kay's solution is much too complicated. Just doing:

#define DEFINE_STRING_ARRAY_HOLDER(name, ...)                 \
    static struct string_holder name##_array[] = __VA_ARGS__; \
    static struct string_array_holder name = { name##_array }

suffices. You may then just use this macro as you intended with:

DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, {
    WRAP_STRING("hello"),
    WRAP_STRING("world")
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文