Argument counting macro with zero arguments for VisualStudio(VisualStudio 的零参数参数计数宏)
问题描述
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
这篇关于VisualStudio 的零参数参数计数宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:VisualStudio 的零参数参数计数宏


基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01