首页 > 解决方案 > 手动代码中带有 C++20 __VA_OPT__ 错误的类函数宏

问题描述

我正在从递归宏和 C++20 的伟大手册构建代码__VA_OPT__https ://www.scs.stanford.edu/~dm/blog/va-opt.html

代码是

#include <iostream>

#define PARENS ()

// Rescan macro tokens 256 times
#define EXPAND(arg) EXPAND1(EXPAND1(EXPAND1(EXPAND1(arg))))
#define EXPAND1(arg) EXPAND2(EXPAND2(EXPAND2(EXPAND2(arg))))
#define EXPAND2(arg) EXPAND3(EXPAND3(EXPAND3(EXPAND3(arg))))
#define EXPAND3(arg) EXPAND4(EXPAND4(EXPAND4(EXPAND4(arg))))
#define EXPAND4(arg) arg

#define FOR_EACH(macro, ...)                                    \
  __VA_OPT__(EXPAND(FOR_EACH_HELPER(macro, __VA_ARGS__)))
#define FOR_EACH_HELPER(macro, a1, ...)                         \
  macro(a1)                                                     \
  __VA_OPT__(FOR_EACH_AGAIN PARENS (macro, __VA_ARGS__))
#define FOR_EACH_AGAIN() FOR_EACH_HELPER

#define ENUM_CASE(name) case name: return #name;

#define MAKE_ENUM(type, ...)                    \
enum type {                                     \
  __VA_ARGS__                                   \
};                                              \
constexpr const char *                          \
to_cstring(type _e)                             \
{                                               \
  using enum type;                              \
  switch (_e) {                                 \
  FOR_EACH(ENUM_CASE, __VA_ARGS__)              \
  default:                                      \
    return "unknown";                           \
  }                                             \
}

MAKE_ENUM(MyType, ZERO, ONE, TWO, THREE);

void
test(MyType e)
{
  std::cout << to_cstring(e) << " = " << e << std::endl;
}

int
main()
{
  test(ZERO);
  test(ONE);
  test(TWO);
  test(THREE);
}

根据https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features__VA_OPT__在 MSVC 和 Clang 中受支持,在 GCC 中仅部分支持。

但令我惊讶的是,手册中的代码仅在 GCC 中有效,在其他编译器中会产生错误:


<source>(36): warning C4003: not enough arguments for function-like macro invocation 'FOR_EACH_HELPER'
<source>(36): warning C4003: not enough arguments for function-like macro invocation 'ENUM_CASE'
<source>(36): error C2059: syntax error: 'case'
<source>(36): error C2065: 'ENUM_CASE': undeclared identifier
<source>(36): error C3861: 'FOR_EACH_HELPER': identifier not found
<source>(36): error C2059: syntax error: ')'
<source>(36): error C2146: syntax error: missing ';' before identifier 'default'

https://gcc.godbolt.org/z/PreYcbz7f

这是否意味着手动代码没有按照 C++20 格式良好,并且需要一些 GCC 特定的语言扩展?

标签: c++g++c++20

解决方案


如果你看这里,我添加了一个__VA_OPT__支持检测器。

#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(?)

static_assert(VA_OPT_SUPPORTED);

gcc 和 clang 通过它;MSVC 版本没有。

然后我查看了 clang 的警告;这是一个警告,告诉您是否将 0 个参数传递给....

它似乎是虚假的。我禁用了它,-Wno-gnu-zero-variadic-macro-arguments你的代码在铿锵声中工作。

因此,godbolt 上的任何版本的 MSVC 似乎都不支持__VA_OPT__,而 gcc 和 clang 版本则支持。并且该 clang 版本在您选择的警告集中有一个虚假警告。

要修复 MSVC,请通过/Zc:preprocessor. 默认情况下,它使用旧的预处理器。


推荐阅读