Templates Versus Generic Classes

Templates generate code in your program each time they are instantiated, so they make the program larger than generic classes. However, generic classes (which leverage class polymorphism) have to use virtual functions, which makes each function call slightly slower.

In any event, you should not let performance drive your decision between templates and class polymorphism unless the problems are severe and you can determine through performance measurements that they are the result of using or not using templates.

Get SAMS Teach Yourself C++ in 10 Minutes SECOND 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.