【问题标题】:How do I have a comma inside braces inside a macro argument when parentheses cause a syntax error?当括号导致语法错误时,如何在宏参数内的大括号内使用逗号?
【发布时间】:2012-02-29 13:13:59
【问题描述】:

我已经定义了一些宏,使定义结构数组变得更加简单,但是我找不到一种方法来使用它们而不产生错误。以下是宏(以及一些示例结构来说明为什么可以使用宏(我填充的实际结构要复杂一些)):

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")
});

我收到此错误:

错误:提供给类函数宏调用的参数过多

因此它将大括号中的逗号解释为参数分隔符。我遵循this question 的建议,并在有问题的论点周围加上括号:

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

现在当我尝试编译它时,它会将({ ... }) 解释为statement expression 并抱怨:

警告:使用 GNU 语句表达式扩展
(由于将其解释为语句表达式而导致的一堆语法错误)
错误: 文件范围内不允许语句表达式

我该怎么做:

  • 使用没有错误的宏(首选),或者
  • 重写宏以在这些情况下工作?

【问题讨论】:

标签: c macros c99 c-preprocessor


【解决方案1】:

Dmitri 是对的,variadic macros 是正确的选择。

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

#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;
}

【讨论】:

  • 实际上,UNWRAP 的东西很整洁。谢谢!
【解决方案2】:

是的,使用__VA_ARGS__,但是Kaysolution 太复杂了。只是在做:

#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")
});

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多