c++11 : Variadic Macros(变长参宏)
Variadic macros are function-like macros that contain a variable number of arguments.
Remarks
To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.
The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma. The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.
Example
// variadic_macros.cpp
#include <stdio.h>
#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 error C2059, except VC++
// suppresses the trailing comma
}
Output
here are some varargs1(1)
here are some varargs2(4)
here are some varargs3(5)
hello, world
error
c++11 : Variadic Macros(变长参宏)的更多相关文章
- 介绍C++11标准的变长参数模板
目前大部分主流编译器的最新版本均支持了C++11标准(官方名为ISO/IEC14882:2011)大部分的语法特性,其中比较难理解的新语法特性可能要属变长参数模板(variadic template) ...
- (一)预定义宏、__func__、_Pragma、变长参数宏定义以及__VA_ARGS__
作为第一篇,首先要说一下C++11与C99的兼容性. C++11将 对以下这些C99特性的支持 都纳入新标准中: 1) C99中的预定义宏 2) __func__预定义标识符 3) _Pragma操作 ...
- [改善Java代码]覆写变长方法也循规蹈矩
建议6:覆写变长方法也循规蹈矩 在Java中,子类覆写父类中的方法很常见,这样做既可以修正Bug也可以提供扩展的业务功能支持,同时还符合开闭原则(Open-Closed Principle),我们来看 ...
- [转载]用可变参数宏(variadic macros)传递可变参数表
注意:_VA_ARGS__ 从VS2005才开始支持 在 GNU C 中,宏可以接受可变数目的参数,就象函数一样,例如: #define pr_debug(fmt,arg...) printk(KER ...
- GNU C和C99标准中的可变参数宏(variadic macros)
用可变参数宏(variadic macros)传递可变参数表你可能很熟悉在函数中使用可变参数表,如: void printf(const char* format, …); 直到最近,可变参数表还是只 ...
- C++11 变长模版和完美转发实例代码
C++11 变长模版和完美转发实例代码 #include <memory>#include <iostream>#include <vector>#include ...
- C++11的模板新特性-变长参数的模板
这个特性很赞,直接给例子吧,假如我要设计一个类,CachedFetcher内部可能使用std::map也可能使用std::unordered_map,也可能是其它的map,怎么设计呢?没有C++11变 ...
- C++11变长参数模板
[C++11变长参数模板] C++03只有固定模板参数.C++11 加入新的表示法,允许任意个数.任意类别的模板参数,不必在定义时将参数的个数固定. 实参的个数也可以是 0,所以 tuple<& ...
- c++11变长参数函数模板
By francis_hao Mar 25,2018 一个最简单的实例大概是这个样子: #include <iostream>using namespace std; /*变长参 ...
随机推荐
- Hyper-V虚拟机和主机的网络配置
Hyper-V虚拟机和主机的网络配置 方式1.共享式 这种方式是将Hyper-V内部的虚拟网络与外部网络共享.使得内部是一个私有的网络.属于NAT的类型.(不知道这么说对不对) 优点: 相对来说属于私 ...
- AppCanCSS背景图片的属性
最近在用AppCan框架技术做跨平台移动应用开发,碰到界面布局设计中图片平铺.拉伸效果. 我们用到的是CSS 3中Background-size属性. 网上查了下这个属性,小记下: 取值: backg ...
- static类
静态类,用于类内部. 静态类,不需要创建父类对象,即可使用. 非静态类,需要先创建父类对象,才可使用. class A{ static class B1{} class B2{} } = new A ...
- #define 和 typedef场合
#define定义“可读”的常量以及一些宏语句的任务,而typedef则常用来定义关键字.冗长的类型的别名.
- RTSP协议资料
维基百科: RTSP:http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol RTP:http://en.wikipedia.org/wik ...
- Property type 'id<tabBarDelegate>' is incompatible with type 'id<UITabBarDelegate> _Nullable' inherited from 'UITabBar'
iOS报错:Property type 'id' is incompatible with type 'id _Nullable' inherited from 'UITabBar' 如图: 可能原因 ...
- C#将图片字节流转为Base64直接放入html的img标签src属性中
1,图片要转为byte[], 2,注意加上“data:image/jpeg;base64,”,这里jpeg可以换成其他. string html = "<img src='data:i ...
- Javascript 中神奇的 this
Javascript 当中的 this 与其他语言是完全不同的机制,很有可能会让一些编写其他语言的工程师迷惑. 1. 误以为 this 指向函数自身 根据 this 的英语语法,很容易将函数中出现的 ...
- 逆天的IE7中,绝对定位元素之间的遮盖问题
个人比较支持IE9以上的版本,认为他们的样式和效果都是比较人性化的,不过很多时候还是不得不考虑其他版本浏览器的感受,这里IE6就不用考虑他了,这货简直就是IT史上的奇葩,这里要说一个IE7的绝对定位和 ...
- Redis同步(主从复制)
目录1.Replication的工作原理2.如何配置Redis主从复制3.应用示例 1.Replication的工作原理在Slave启动并连接到Master之后,它将主动发送一条SYNC命令.此后Ma ...