Chapter 10. Templates

The same code in different contexts can lead to very different operations. For example, x+y can mean integer addition, floating-point addition, string concatenation, and a number of other things, depending on the types of x and y. This is sometimes called compile-time polymorphism, to distinguish it from runtime polymorphism, which is what happens with classes that have virtual methods. In runtime polymorphism the exact function to call is decided at the last moment, whereas the operation meant by x+y is worked out by the compiler.

The C++ template mechanism takes advantage of the fact that operations can be expressed so abstractly. Templates are generators of other types, not types themselves.

In this final chapter, you ...

Get C++ By Example: UnderC Learning 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.