www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 764 (continued)]

Summary

  • Templates enable us to specify a range of related (overloaded) functionscalled function-template specializationsor a range of related classescalled class-template specializations.

  • To use function-template specializations, the programmer writes a single function-template definition. Based on the argument types provided in calls to this function, C++ generates separate specializations to handle each type of call appropriately. These are compiled along with the rest of a program's source code.

  • All function-template definitions begin with the keyword template followed by template parameters to the function template enclosed in angle brackets (< and >); each template parameter that represents a type must be preceded by keyword class or typename. Keywords typename and class used to specify function-template parameters mean "any built-in type or user-defined type."

  • Template-definition template parameters are used to specify the kinds of arguments to the function, the return type of the function and to declare variables in the function.

  • The name of a template parameter can be declared only once in the type-parameter list of a template header. Formal type-parameter names among function templates need not be unique.

  • A function template may be overloaded in several ways. We can provide other function templates that specify the same function name but different function parameters. A function template can also be overloaded by providing other nontemplate functions with the same function name, but different function parameters.

  • Class templates provide the means for describing a class generically and for instantiating classes that are type-specific versions of this generic class.

  • Class templates are called parameterized types; they require type parameters to specify how to customize a generic class template to form a specific class-template specialization.


  • [Page 765]
  • The programmer who wishes to use class-template specializations writes one class template. When the programmer needs a new type-specific class, the programmer uses a concise notation, and the compiler writes the source code for the class-template specialization.

  • A class-template definition looks like a conventional class definition, except that it is preceded by template< typename T > (or template< class T >) to indicate this is a class-template definition with type parameter T which acts as a placeholder for the type of the class to create. The type T is mentioned throughout the class header and member-function definitions as a generic type name.

  • Member-function definitions outside a class template each begin with template< typename T > (or template< class T >). Then, each function definition resembles a conventional function definition, except that the generic data in the class always is listed generically as type parameter T. The binary scope-resolution operator is used with the class-template name to tie each member function definition to the class template's scope.

  • It is possible to use nontype parameters in the header of a class or function template.

  • An explicit specialization of a class template can be provided to override a class template for a specific type.

  • A class template can be derived from a class-template specialization. A class template can be derived from a nontemplate class. A class-template specialization can be derived from a class-template specialization. A nontemplate class can be derived from a class-template specialization.

  • Functions and entire classes can be declared as friends of nontemplate classes. With class templates, the obvious kinds of friendship arrangements can be declared. Friendship can be established between a class template and a global function, a member function of another class (possibly a class-template specialization) or even an entire class (possibly a class-template specialization).

  • Each class-template specialization instantiated from a class template has its own copy of each static data member of the class template; all objects of that specialization share that static data member. And as with static data members of nontemplate classes, static data members of class-template specializations must be defined and, if necessary, initialized at file scope.

  • Each class-template specialization gets a copy of the class template's static member functions.


Previous Page
Next Page