VisualStudio 的零参数参数计数宏

2022-01-11 00:00:00 macros visual-c++ c++

gcc 确实支持使用 ## __VA_ARGS__ 约定的零参数的参数计数宏.以下作品用 gcc 编译:

gcc does support argument counting macros with zero arguments with the ## __VA_ARGS__ convention. The following works compiled with gcc:

#include <stdio.h>

#define NARGS(...) __NARGS(0, ## __VA_ARGS__, 5,4,3,2,1,0)
#define __NARGS(_0,_1,_2,_3,_4,_5,N,...) N

int main()
{
  printf("%d
", NARGS());     // prints 0
  printf("%d
", NARGS(1));    // prints 1
  printf("%d
", NARGS(1, 2)); // prints 2
  return 0;
}

是否有适用于零参数宏的 VisualC++ 等价物?接受非标准的扩展或技巧.

Is there an equivalent for VisualC++ that will work with zero arguments macros? Non standard extensions or tricks accepted.

编辑:修复了示例以使用 GCC 扩展和 C++ 编译器.

EDIT: Fixed the example to work with GCC extensions and C++ compiler.

推荐答案

以下示例在 VisualStudio 2010 及更高版本、启用非标准扩展的 gcc 和 clang 中运行良好.在 Microsoft 编译器中,它假定当参数计数为零时,预处理器将删除 AUGMENTER 宏中的尾随逗号.这是非标准的,其他地方也有报道.在 gcc 和 clang 中,它使用广为人知的 ## __VA_ARGS__ 非标准扩展.

The following example works fine in VisualStudio 2010 and newer, gcc and clang with non standard extensions enabled. In Microsoft compilers it assumes the trailing comma in the AUGMENTER macro will be removed by the preprocessor when arguments count is zero. This is non standard and it has been also reported elsewere. In gcc and clang it uses the widely known ## __VA_ARGS__ non standard extension.

#include <stdio.h>

#ifdef _MSC_VER // Microsoft compilers

#define EXPAND(x) x
#define __NARGS(_1, _2, _3, _4, _5, VAL, ...) VAL
#define NARGS_1(...) EXPAND(__NARGS(__VA_ARGS__, 4, 3, 2, 1, 0))

#define AUGMENTER(...) unused, __VA_ARGS__
#define NARGS(...) NARGS_1(AUGMENTER(__VA_ARGS__))

#else // Others

#define NARGS(...) __NARGS(0, ## __VA_ARGS__, 5,4,3,2,1,0)
#define __NARGS(_0,_1,_2,_3,_4,_5,N,...) N

#endif

int main()
{
  // NARGS
  printf("%d
", NARGS());          // Prints 0
  printf("%d
", NARGS(1));         // Prints 1
  printf("%d
", NARGS(1, 2));      // Prints 2
  fflush(stdout);

#ifdef _MSC_VER
  // NARGS minus 1
  printf("
");
  printf("%d
", NARGS_1(1));       // Prints 0
  printf("%d
", NARGS_1(1, 2));    // Prints 1
  printf("%d
", NARGS_1(1, 2, 3)); // Prints 2
#endif

  return 0;
}

使用真正的编译器测试宏,Wandbox 和 网页编译器

Macros were tested with real compilers, Wandbox and Webcompiler

相关文章