Template functions

In addition to regular functions, for which the parameter types are known, C++ also has template functions. When these functions are called, the parameter types are deduced from the types of the arguments at the call site. The template functions can have the same name as the non-template functions, and several template functions can have the same name as well, so we need to learn about overload resolution in the presence of templates.

Consider the following example:

void f(int i) { std::cout << "f(int)" << std::endl; }                  // 1void f(long i) { std::cout << "f(long)" << std::endl; }                // 2template <typename T> void f(T i) { std::cout << "f(T)" << std::endl; }// 3f(5);      // 1f(5l);     // 2f(5.0);    // 3

The f function name can refer ...

Get Hands-On Design Patterns with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.