Default Template Argument

Default Template Argument

Like ordinary functions, templates can have default type arguments. A good example is STL’s vector. It actually has two arguments–the second one is an allocator class template. Since an allocator is something you should rarely care about, it has a default value: the implementation’s allocator, which spares you the bother of specifying an allocator type every time you use it:

 template  > //definition of the standard vector container	class vector { /*_*/ };

The default argument enables you to use vector like this:

 vector vi; //second template arguments needn't be supplied since it has a default value


Share the Post: