`

c++ - class template default parameters

    博客分类:
  • c++
c++ 
阅读更多

the template has parameter, it can contains type paramter and it can also have nontype paramter, in the following code. 

 

template <class Type, int size>
class Buffer;

 

the class template buffer has two paramter, one is the non-determinic parameter, which is Type, and one nontype (which means the type itself is bound and known, which remains unknown is only the vlaue of the nontype parameter).

 

As for the function parameter, you can also provide default value for template class, for the type paramter, the default value is type, while for the non-type parameter, the default value is value.

 

Let's see the example below. 

 

/**
* file: 
*  default_template_parameters.h
*
* description:
*  this file shows that you can provide default paramter value for either the type parameter or the nontype parameter
*/

#include<string>
using std::string;

template <class Type, int size = 1024>
class Buffer;

template <class Type = string, int size /* you don't need to provide "= 1024" */>
class Buffer;
//template <class Type = string, int size = 1024>
//class Buffer;

template <class Type, int size>
class Buffer
{
public:
	Buffer() : _size(size) {}
private:
	Type elem;
	int _size;
protected:
};
 
分享到:
评论

相关推荐

    Google C++ Style Guide_英文版.pdf

    - **Class Template Argument Deduction:** Use CTAD (Class Template Argument Deduction) to make template class instantiation more concise. - **Designated Initializers:** Use designated initializers for ...

    Bloodshed Dev-C++

    * During Dev-C++ First Time COnfiguration window, users can now choose between using or not class browser and code completion features. * Many bug fixes Version 4.9.8.4 * Added the possibility to ...

    C++ 备忘单-.pdf

    - 类模板:`template&lt;typename T&gt; class ClassName { ... };` 3. **Lambda 表达式**: - 匿名函数:`[]() { ... };` 4. **类型推断**: - `auto` 关键字用于自动类型推断 5. **范围 for 循环**: - `for ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Other C++ Features Reference Arguments Function Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and ...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    - **Template Class stack:** Explains template classes with an example of a stack. - **Function Templates:** Describes function templates. - **Generic Code Development: Quicksort:** Provides a generic ...

    Problem Solving with C++ (7th edition)

    This chapter covers the Standard Template Library (STL), a powerful set of C++ libraries: - **STL Containers**: Explanation of STL containers, such as vectors, lists, and maps. - **STL Algorithms**: ...

    c++基础知识总结概括

    - `default`: 在开关语句中表示默认情况。 - `goto`: 无条件跳转到标记的位置。 - `sizeof`: 返回数据类型的大小(字节数)。 - `volatile`: 表示变量可能被意外地改变。 - `do`: 循环结构的关键字,先执行后判断。 ...

    Rad Studio Delphi C++builder XE 10.4 Patch2

    RSP-29256 Compiler generates wrong code for template function RSP-29227 Incorrect property value obtained from the record RSP-29226 Access violation with working code under 10.2 RSP-29218 compiling ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The TFlexPanel.FPaintCache field moved in the protected class section. Added rcPaint field in FPaintCache that represents drawing rectangle. - ADD: In the text prcise mode (TFlexText.Precise=...

    Effective C++(第三版)

    declare non-member functions when type conversions should apply to all parameters. 条款25:考虑写出一个不抛异常的swap函数 consider support for a non-throwing swap. 5. 实现 implementations 条款26:尽...

    c++出错提示英汉对照表

    根据给定的C++错误提示英汉对照表,我们可以详细解析并总结出一系列与C++编程相关的常见错误及其解决方法,这对于理解和调试C++代码至关重要。 1. **Ambiguous operators need parentheses** - 模糊的操作符需要...

    《面向对象程序设计C++》期末试卷及标准答案(B).docx

    3. **默认参数(Default Parameters)**:函数可以有默认参数,如`void testDefaulParam(int a, int b=7, char z='*')`,调用时如果省略了参数,会使用默认值。 4. **重载函数(Overload Function)**:重载函数...

    Google C++ International Standard.pdf

    4.4 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.5 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    STL源码剖析.pdg

    组态10:default template argument depend on previous template parameters 030 组态11:non-type template parameters 031 组态:bound friend template function 032 组态:class template explicit ...

    EurekaLog_7.5.0.0_Enterprise

    33)..Changed: Default template of HTML/web dialog now includes call stack by default 34)..Changed: EurekaLog 7 now can be installed over EurekaLog 6 automatically, with no additional actions/tools ...

    STL 源码剖析(侯捷先生译著)

    组态10:default template argument depend on previous template parameters 030 组态11:non-type template parameters 031 组态:bound friend template function 032 组态:class template explicit ...

    vxworks_kernel_programmers_guide_6.9

    2.2.1 Default Configuration and Images ................................................................. 8 2.2.2 Configuration With VxWorks Image Projects ................................................

Global site tag (gtag.js) - Google Analytics