【发布时间】:2015-03-20 06:55:42
【问题描述】:
我似乎无法将参数传递给期望不同参数的函数(或其他实现第一个类型子集的 _Generic 宏)。
#define DEBUG_PRINT(x,...) _Generic((x), \
debug_print_options *: DEBUG_PRINT_CUSTOM_TYPE(x, __VA_ARGS__), \
default: DEBUG_PRINT_BASIC_TYPE(x, __VA_ARGS__))
#define DEBUG_PRINT_BASIC_TYPE(x,...) debug_print_printf_specifier((#x), (x), TYPE_TO_PRINTF_SPECIFIER(x), __FILE__, __LINE__, _my_func__, &((struct debug_print_options){__VA_ARGS__}))
#define DEBUG_PRINT_CUSTOM_TYPE(x,...) debug_print_custom_to_debug_string((#x), (x), GET_CREATE_DEBUG_STRING_FUNC(x), __FILE__, __LINE__, _my_func__, &((struct debug_print_options){__VA_ARGS__}))
给出编译错误:
debug_print.h:123:46: error: ‘_Generic’ selector of type ‘struct debug_print_options *’ is not compatible with any association
这使它看起来好像在每个分支机构都得到了评估。如果我注释掉它编译的默认值。
有没有办法解决这个问题?
【问题讨论】:
-
另外,如果每个分支都被评估,为什么???
-
Incompatible pointer types passing in _Generic macro 的可能重复项,并回答您的“为什么”问题:如果您有类似
if(0) something; else other;的内容,这也是一样的,所有分支也必须有效。将其视为一项功能,可能所有分支都可能在某些平台上处于活动状态,因此最好从语法上检查它们。