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); ...

Get C++ Templates: The Complete Guide, 2nd Edition 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.