7. Templates and Generic Programming

The initial motivation for C++ templates was straightforward: to make it possible to create type-safe containers like vector, list, and map. The more people worked with templates, however, the wider the variety of things they found they could do with them. Containers were good, but generic programming — the ability to write code that is independent of the types of objects being manipulated — was even better. STL algorithms like for_each, find, and merge are examples of such programming. Ultimately, it was discovered that the C++ template mechanism is itself Turing-complete: it can be used to compute any computable value. That led to template metaprogramming: the creation of programs that execute inside C++ ...

Get Effective C++: 55 Specific Ways to Improve Your Programs and Designs, Third 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.