x

C++ Templates

PREV     NEXT

What are Templates?

  • Template is an important and powerful feature in C++. It is used to pass the data type as a parameter so that you don’t need to write same code for different data types.
  • It allows you to define the generic classes and generic functions and thus provides support for generic programming.
  • For example if you need to sort( ) for different data type rather than writing and maintaining the multiple codes you can write one sort( ) and pass data type as a parameter.

How Templates work?

  • The templates are expanded at the compiler time and they are like macros. The difference is that compiler does type checking before template expansion.
  • The source code contains only function or class but compiled code may contain multiple copies of same function or class.

Types of Template