Item 61. You Instantiate What You Use

In both C and C++, if you don’t call a declared function (or take its address), you don’t have to define it. An analogous situation occurs with member functions of class templates; if you don’t actually call a template’s member function, it’s not instantiated.

This is clearly a handy property for the purpose of reducing code size. If a class template defines a large number of member functions, but you use only two or three of them, you don’t pay the code space penalty for all those unused functions.

An even more important result of this rule is that you can specialize class templates with arguments that would be illegal if all the member functions were instantiated. With this rule in place, it’s possible ...

Get C++ Common Knowledge: Essential Intermediate Programming 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.