O'Reilly logo

C++ Templates: The Complete Guide, 2nd Edition by Douglas Gregor, Nicolai M. Josuttis, David Vandevoorde

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

Chapter 20

Overloading on Type Properties

Function overloading allows the same function name to be used for multiple functions, so long as those functions are distinguished by their parameter types. For example:

void f (int); void f (char const*);

With function templates, one overloads on type patterns such as pointer-to-T or Array<T>:

template<typename T> void f(T*); template<typename T> void f(Array<T>);

Given the prevalence of type traits (discussed in Chapter 19), it is natural to want to overload function templates based on the properties of the template arguments. For example:

template<typename Number> void f(Number);  // only for numbers template<typename Container> void f(Container); ...

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