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 ...
随机推荐
- 议题解析与复现--《Java内存攻击技术漫谈》(二)无文件落地Agent型内存马
无文件落地Agent型内存马植入 可行性分析 使用jsp写入或者代码执行漏洞,如反序列化等,不需要上传agent Java 动态调试技术原理及实践 - 美团技术团队 (meituan.com) 首先, ...
- gitbook的安装
[前端工具]nodejs+npm+vue 安装 安装 npm install gitbook-cli -g gitbook命令: gitbook init //初始化目录文件 gitbook help ...
- AliRTC 开启视频互动 “零计算” 时代
在 2021 云栖大会<产业视频化创新与最佳实践>视频云主题论坛中,阿里云智能高级技术专家在<AliRTC 开启视频互动 "零处理" 时代>的主题演讲中,发 ...
- Qt 信号与槽的自动关联机制
前言 对于一些简单的事件判别,如点击按钮.无需写代码关联信号和槽函数. connect(ui->Btnshowhello,SIGNAL(clicked(bool)),this,SLOT(Btns ...
- 难顶!面试官问我G1垃圾收集器
面试官:要不这次来聊聊G1垃圾收集器? 候选者:嗯嗯,好的呀 候选者:上次我记得说过,CMS垃圾收集器的弊端:会产生内存碎片&&空间需要预留 候选者:这俩个问题在处理的时候,很有可能会 ...
- Spring 为啥默认把bean设计成单例的?
熟悉Spring开发的朋友都知道Spring提供了5种scope分别是singleton.prototype.request.session.global session. 如下图是官方文档上的截图, ...
- 通过实现仿照FeignClient框架原理的示例来看清FeignClient的本质
前言 FeignClient的实现原理网上一搜一大把,此处我就不详细再说明,比如:Feign原理 (图解) - 疯狂创客圈 - 博客园 (cnblogs.com),而且关于FeignClient的使用 ...
- Python布尔值
在学到Python数据类型时,发现与大多数语言没什么区别 布尔值可以用 and or not 来运算 and运算是与运算,所有条件都符合才为true >>> True and Tru ...
- SpringCloud升级之路2020.0.x版-38. 实现自定义 WebClient 的 NamedContextFactory
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 实现 WeClient 的 NamedContextFactory 我们要实现的是不同微服 ...
- [hdu7000]二分
不妨假设$x\le y$,可以通过翻转整个格子序列来调整 令$a_{i}$为$i$到$y$的期望步数,显然有方程$a_{i}=\begin{cases}0&(i=y)\\\frac ...