O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

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

Template Functions

A template function is an abstraction of a function that fulfils a specific task. It is a function that accepts generic types, or, rather, it is a function that is defined with a placeholder name instead of a type that can adapt, at compile time, to any type that the programmer needs.

The general principal is that the types are determined by the compiler by examining the code. The compiler will then substitute the types into the function at the appropriate places, and as long as the operations are compatible with the types supplied in the function call, the code will compile correctly.

The parameters of the template function are generic, not the function itself—a common misconception of newcomers. The mechanism allows you ...

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