可移植地检测__VA_OPT__支持吗? [英] Portably detect __VA_OPT__ support?
问题描述
在C ++ 20中,预处理器支持 __ VA_OPT __
作为在参数数大于零的情况下有选择地扩展可变参宏中的标记的一种方法。 (这消除了对 ## __ VA_ARGS __
GCC扩展的需要,这是一个不可移植且难看的hack。)
In C++20, the preprocessor supports __VA_OPT__
as a way to optionally expand tokens in a variadic macro if the number of arguments is greater than zero. (This obviates the need for the ##__VA_ARGS__
GCC extension, which is a non-portable and ugly hack.)
Clang SVN已实现此功能,但他们尚未为其添加功能测试宏。任何聪明的预处理程序黑客都能找到一种方法来检测是否存在 __ VA_OPT __
支持而不会引起硬错误或可移植性警告吗?
Clang SVN has implemented this feature, but they haven't added a feature test macro for it. Can any clever preprocessor hacker figure out a way to detect the presence or absence of __VA_OPT__
support without causing a hard error or a portability warning?
推荐答案
受 chris's answer 的启发。
#define PP_THIRD_ARG(a,b,c,...) c
#define VA_OPT_SUPPORTED_I(...) PP_THIRD_ARG(__VA_OPT__(,),true,false,)
#define VA_OPT_SUPPORTED VA_OPT_SUPPORTED_I(?)
如果 __ VA_OPT __
受支持, VA_OPT_SUPPORTED_I(?)
扩展为 PP_THIRD_ARG(,, true,false,)
,因此第三个参数为 true
;否则, VA_OPT_SUPPORTED_I(?)
扩展为 PP_THIRD_ARG(__ VA_OPT __(,),true,false,)
,第三个参数是 false
。
If __VA_OPT__
is supported, VA_OPT_SUPPORTED_I(?)
expands to PP_THIRD_ARG(,,true,false,)
, so the third argument is true
; otherwise, VA_OPT_SUPPORTED_I(?)
expands to PP_THIRD_ARG(__VA_OPT__(,),true,false,)
, the third argument is false
.
这篇关于可移植地检测__VA_OPT__支持吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!