`

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:
};
 
分享到:
评论

相关推荐

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

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

    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:尽...

    Google C++ International Standard.pdf

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

    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源码剖析.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 ...

    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