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的更多相关文章

  1. 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") ...

  2. 类模板成员函数默认值问题: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编译器会报 ...

  3. [Python] Problem with Default Arguments

    Default arguments are a helpful feature, but there is one situation where they can be surprisingly u ...

  4. Default arguments and virtual function

    Predict the output of following C++ program. 1 #include <iostream> 2 using namespace std; 3 4 ...

  5. :(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){

  6. Use default arguments instead of short circuiting or conditionals使用默认实参代替短路和条件

  7. 六. Default arguments 参数默认值

    示例: 注意点:函数是会默认声明参数变量的,所以不需要再重新声明一次,否则会报错 错误示例如下: 函数参数的传值方法: 需要注意的是:如果要给第二个参数传值,那第一个参数要传undefined,而不能 ...

  8. 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 ...

  9. ES6 new syntax of Default Function Parameters

    Default Function Parameters.md Default Function Parameters function getSum(a,b){ a = (a !== undefine ...

随机推荐

  1. JMeter 5.4 打开测试计划 报错:Unexpected error

    保存测试计划名为: 新增用户.jmx 关闭 JMeter 再次打开JMeter : 双击 jmeter.bat JMeter 启动 打开 新增用户.jmx Unexpected error. 看上图 ...

  2. css书写规范 & 页面布局规范 &常用案例经验总结

    CSS 属性书写顺序(重点) 建议遵循以下顺序: 布局定位属性:display / position / float / clear / visibility / overflow(建议 displa ...

  3. 『与善仁』Appium基础 — 8、Appium自动化测试框架介绍

    目录 1.主流的移动端自动化测试框架 (1)Robotium (2)Macaca (3)Appium 2.自动化测试工具的选择 3.Appium简介 提示:我们前面说的Android环境搭建和adb命 ...

  4. [Apache Doris] Apache Doris 元数据设计及DDL操作源码阅读

    元数据设计 如上图,Doris 的元数据主要存储4类数据: 用户数据信息.包括数据库.表的 Schema.分片信息等. 各类作业信息.如导入作业,Clone 作业.SchemaChange 作业等. ...

  5. [cf566C]Logistical Questions

    记$d(x,y)$为$x$到$y$的距离,$cost_{x}=\sum_{i=1}^{n}w_{i}d(x,i)^{\frac{3}{2}}$为$x$的代价 取$C$为足够大量,对于一条边权为$w$的 ...

  6. [atARC110E]Shorten ABC

    考虑令$a$.$b$和$c$分别对应1.2和3,那么每一次相当于令$x$和$y$变为$x\oplus y$(要求$x\ne y$) 根据异或的结合律,我们相当于将其划分为若干个区间求异或值 (另外还有 ...

  7. [bzoj1084]最大子矩阵

    用f[i][j][k]表示第一行前i个数,第二行前j个数选k个子矩形的答案,考虑转移:1.在第一行/第二行选择一个矩形2.当i=j时,可以选择一个两行的矩形注意要特判m=1的情况 1 #include ...

  8. [loj3046]语言

    定义$S_{i}$表示第$i$条链所包含的点的集合,$(x,y)$合法当且仅当$x\ne y$且$\exists i,\{x,y\}\subseteq S_{i}$(答案即$\frac{合法点对数}{ ...

  9. SSM整合小项目

    1.文件目录结构 2.MyBatis配置 创建数据库环境 CREATE DATABASE `ssmbuild`; USE `ssmbuild`; DROP TABLE IF EXISTS `books ...

  10. radio两行每行只能选择一个的解决方案!

    如图,我要做到这个效果,竖着每行只能有一个最像,和最不像,点击左边禁用右边 <div v-else> <div v-if="progress<quiz.length& ...