Dec 21, 2008

Template

Template

function template
template function_declaration;
template function_declaration;//no difference

class template
should be defined inline within the class declaration, otherwise should be preceded the definition with the template <…> prefix

Template specialization
the different implementation for a template when a specific type is passed as template parameter, we can define template specialization
generic class template and the specialization:
template class mycontainer { ... };
template <> class mycontainer { ... };
default parameter for template
template class mysequence {..};

disadvantage of template
Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

Resource :
http://www.cplusplus.com/doc/tutorial/templates.html

No comments:

Post a Comment