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 ...
随机推荐
- Redis网络库源码分析(3)之ae.c
一.aeCreateEventLoop & aeCreateFileEvent 上一篇文章中,我们已经将服务器启动,只是其中有些细节我们跳过了,比如aeCreateEventLoop函数到底做 ...
- JMeter源码导入到Intellij IDEA (八)
亲测:jmeter源码导入Intellij IDEA,导入,编译,运行! 一.环境:windows10操作系统,jdk1.8,Intellij IDEA 2018.3,jmeter5.0 下载jmet ...
- WebRTC概念介绍
WebRTC WebRTC(Web Real-Time Communication).Real-Time Communication,实时通讯. WebRTC能让web应用和站点之间选择性地分享音视频 ...
- springboot注解之容器功能
添加组件 @Configuration.@Bean //以swagger为例 @Configuration(proxyBeanMethods = false) @EnableSwagger2 //使用 ...
- 微信小程序(八)
应用弹性盒子布局 基于 flexbox layout 的实现 先变为 flexbox layout display: flex; 从上往下 flex-direction: column; 均匀分布,居 ...
- php 递推 递归
思想:如何利用数学模式,来解决对应的需求问题,然后利用代码实现对应的数据模型(逻辑) 算法:使用代码实现对应的数学模型,从而解决对应的业务问题 递推算法是一种简单的算法,级通过已知条件,利用特定关系得 ...
- Android学习—下载Android SDK的两种方式
在Android Studio中下载Android SDK的两种方式 Android studio下载地址:http://www.android-studio.org/ 方式一.设置HTTP Prox ...
- 面试官:Java 线程如何启动的?
摘要:Java 的线程创建和启动非常简单,但如果问一个线程是怎么启动起来的往往并不清楚,甚至不知道为什么启动时是调用start(),而不是调用run()方法呢? 本文分享自华为云社区<Threa ...
- 【POJ3614 Sunscreen】【贪心】
题面: 有c头牛,需要的亮度在[min_ci,max_ci]中,有n种药,每种m瓶,可以使亮度变为v 问最多能满足多少头牛 算法 我们自然考虑贪心,我们首先对每头牛的min进行排序,然后对于每种药,将 ...
- LOJ 2353 & 洛谷 P4027 [NOI2007]货币兑换(CDQ 分治维护斜率优化)
题目传送门 纪念一下第一道(?)自己 yy 出来的 NOI 题. 考虑 dp,\(dp[i]\) 表示到第 \(i\) 天最多有多少钱. 那么有 \(dp[i]=\max\{\max\limits_{ ...