设为首页 加入收藏

TOP

可变参数宏__VA_ARGS__
2014-11-23 23:39:53 来源: 作者: 【 】 浏览:7
Tags:可变 参数 __VA_ARGS__


宏定义使用可变参数,使用起来方便多了。具体看msdn例子:

Support for variadic macros was introduced in Visual C++ 2005.

// variadic_macros.cpp
#include
#define EMPTY

#define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); }
#define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); }
#define CHECK3(...) { printf(__VA_ARGS__); }
#define MACRO(s, ...) printf(s, __VA_ARGS__)

int main() {
CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)\n");
CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)\n"); // won't print

CHECK2(0, "here %s %s %s", "are", "some", "varargs2(3)\n"); // won't print
CHECK2(1, "here %s %s %s", "are", "some", "varargs2(4)\n");

// always invokes printf in the macro
CHECK3("here %s %s %s", "are", "some", "varargs3(5)\n");

MACRO("hello, world\n");
// MACRO("error\n", EMPTY); would cause C2059
}

here are some varargs1(1)
here are some varargs2(4)
here are some varargs3(5)
hello, world

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇类大小测试(空类、带含静态和非.. 下一篇DOS下显示8位256色BMP位图

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: