O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Code walkthrough

The following code is a non-template version of our custom sort()  function:

void sort ( array<int,6> data ) {      cout << "Non-template sort function invoked ..." << endl;      int size = data.size();     for ( int i=0; i<size; ++i ) {          for ( int j=0; j<size; ++j ) {             if ( data[i] < data[j] )                 swap ( data[i], data[j] );         }     }}

Non-template functions and template functions can coexist and participate in function overloading. One weird behavior of the preceding function is that the size of the array is hardcoded.

The second version of our sort() function is a template function, as shown in the following code snippet. Interestingly, the weird issue that we noticed in the first non-template sort() version is addressed here:

template <typename ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required