template

Syntax:

    template <class data-type> return-type name( parameter-list ) {
    statement-list;
    }

Templates are used to create generic functions and can operate on data without knowing the nature of that data. They accomplish this by using a placeholder data-type for which many other data types can be substituted. Example code: For example, the following code uses a template to define a generic swap function that can swap two variables of any type:

     template<class X> void genericSwap( X &a, X &b ) {
       X tmp;
       tmp = a;
       a = b;
       b = tmp;
     }
     int main(void) {
       ...
       int num1 = 5;
       int num2 = 21;
       cout << "Before, num1 is " << num1 << " and num2 is " << num2 << endl;
       genericSwap( num1, num2 );
       cout << "After, num1 is " << num1 << " and num2 is " << num2 << endl;
       char c1 = 'a';
       char c2 = 'z';
       cout << "Before, c1 is " << c1 << " and c2 is " << c2 << endl;
       genericSwap( c1, c2 );
       cout << "After, c1 is " << c1 << " and c2 is " << c2 << endl;
       ...
       return( 0 );
     }

Related Topics: typename