gpt4 book ai didi

c - 当括号导致语法错误时,如何在宏参数内的大括号内使用逗号?

转载 作者:太空狗 更新时间:2023-10-29 16:35:59 24 4
gpt4 key购买 nike

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

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

我收到这个错误:

error: too many arguments provided to function-like macro invocation

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

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

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

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

我怎样才能:

  • 正确使用宏(首选),或者
  • 重写宏以在这些情况下工作?

最佳答案

德米特里是对的,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__,一个粗略的解决方案是一个“unwrapper”宏:

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

关于c - 当括号导致语法错误时,如何在宏参数内的大括号内使用逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9187614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com