Templates and Default Arguments
Default parameters for templates in C++:
Like function default arguments, templates can also have default arguments. For example, in the following program, the second parameter U has the default value as char.
1 #include<iostream>
2 using namespace std;
3
4 template<class T, class U = char> class A
5 {
6 public:
7 T x;
8 U y;
9 };
10
11 int main()
12 {
13 A<char> a;
14 A<int, int> b;
15 cout<<sizeof(a)<<endl;
16 cout<<sizeof(b)<<endl;
17 return 0;
18 }
Output: (char takes 1 byte and int takes 4 bytes)
2
8
Also, similar to default function arguments, if one template parameter has a default argument, then all template parameters following it must also have default arguments.
For example, the compiler will not allow the following program:
1 #include<iostream>
2 using namespace std;
3
4 template<class T = char, class U, class V = int> class A // Error
5 {
6 // members of A
7 };
8
9 int main()
10 {
11
12 }
template的默认参数列表规则与函数的默认参数列表规则一样。
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 22:21:23
Templates and Default Arguments的更多相关文章
- scala - multiple overloaded alternatives of method bar define default arguments
同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...
- 类模板成员函数默认值问题:an out-of-line definition of a member of a class template cannot have default arguments
template <typename T> class A { ); }; template<typename T> ) { /* */ } 对于类似上文代码,VS编译器会报 ...
- [Python] Problem with Default Arguments
Default arguments are a helpful feature, but there is one situation where they can be surprisingly u ...
- Default arguments and virtual function
Predict the output of following C++ program. 1 #include <iostream> 2 using namespace std; 3 4 ...
- :(23, 7) in class Queen, multiple overloaded alternatives of constructor Queen define default arguments. class Queen private(val name:String,prop:Array[String],private[scala02] val age:Int = 18){
- Use default arguments instead of short circuiting or conditionals使用默认实参代替短路和条件
- 六. Default arguments 参数默认值
示例: 注意点:函数是会默认声明参数变量的,所以不需要再重新声明一次,否则会报错 错误示例如下: 函数参数的传值方法: 需要注意的是:如果要给第二个参数传值,那第一个参数要传undefined,而不能 ...
- Default Parameter Values in Python
Python’s handling of default parameter values is one of a few things that tends to trip up most new ...
- ES6 new syntax of Default Function Parameters
Default Function Parameters.md Default Function Parameters function getSum(a,b){ a = (a !== undefine ...
随机推荐
- BugKu之备份是个好习惯
题目:备份是个好习惯 思路分析 打开题目,看到一个字符串. 联系到题目,就猜到肯定是源代码泄露,用工具扫一下,发现了index.php.bak,验证了我的猜想,下载下来看看. <?php /** ...
- pip 安装使用 ImportError: No module named setuptools 解决方法
安装过程详见这篇博客: http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa/ 安装后运行到:python setup.py ins ...
- linux网络编程 IO多路复用 select epoll
本文以我的小型聊天室为例,对于服务器端的代码,做了三次改进,我将分别介绍阻塞式IO,select,epoll . 一:阻塞式IO 对于聊天室这种程序,我们最容易想到的是在服务器端accept之后,然后 ...
- 攻防世界 WEB 高手进阶区 XCTF 4th-CyberEarth ics-06 Writeup
攻防世界 WEB 高手进阶区 XCTF 4th-CyberEarth ics-06 Writeup 题目介绍 题目考点 掌握暴力破解手段 Writeup 打开链接 http://220.249.52. ...
- Pytorch中stack()方法的理解
Torch.stack() 1. 概念 在一个新的维度上连接一个张量序列 2. 参数 tensors (sequence)需要连接的张量序列 dim (int)在第dim个维度上连接 注意输入的张量s ...
- Java oop三大特性(封装,继承,多态)
封装 顾名思义,就是将数据封装起来,提高数据的安全性.我们程序都是要追求"高内聚,低耦合".高内聚就是类的内部数据操作细节自己完成,不允许外部干涉,低耦合:仅暴露少量的方法给外部使 ...
- flask cache
http://brunorocha.org/python/flask/using-flask-cache.html 如何在大项目中使用cache 新建全局cache.py cache = Cache( ...
- [cf1495E]Qingshan and Daniel
选择其中卡片总数较少的一类,当相同时选择$t_{1}$所对应的一类(以下记作$A$类) 如果$t_{1}$不是$A$类,就先对$t_{1}$操作一次(即令$a_{1}$减少1) 下面,问题即不断删去$ ...
- [loj3368]数蘑菇
由于题目是让我们统计个数,当我们确定了$k$个$p_{i}$都为0或1后,再用至多$\lceil \frac{n-k}{k}\rceil$次询问和$2(n-k)$个"$n$"即可求 ...
- spring boot 动态生成接口实现类
目录 一: 定义注解 二: 建立动态代理类 三: 注入spring容器 四: 编写拦截器 五: 新建测试类 在某些业务场景中,我们只需要业务代码中定义相应的接口或者相应的注解,并不需要实现对应的逻辑. ...