66. Don’t specialize function templates

Summary

Specialization is good only when it can be done correctly: When extending someone else’s function template (including std::swap), avoid trying to write a specialization; instead, write an overload of the function template, and put it in the namespace of the type(s) the overload is designed to be used for. (See Item 57.) When you write your own function template, avoid encouraging direct specialization of the function template itself.

Discussion

It’s okay to overload function templates. Overload resolution considers all primary templates equally, and that’s why it works as you would naturally expect from your experience with normal C++ function overloading: Whatever templates are visible are considered ...

Get C++ Coding Standards: 101 Rules, Guidelines, and Best Practices 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.