|
|
|
|
|
|
|
|
|
|
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 for overload resolution, and the compiler simply picks the best match. Unfortunately, it's a lot less intuitive to specialize function templates. There are two basic reasons: • You can't specialize function templates partially, only totally: Code that looks like partial specialization is really just overloading instead. • Function template specializations never participate in overloading: Therefore, any specializations you write will not affect which template gets used, and this runs counter to what most people would intuitively expect. After all, if you had writ ten a nontemplate function with the identical signature instead of a function template specialization, the nontemplate function would always be selected be cause it's always considered to be a better match than a template. If you're writing a function template, prefer to write it as a single function template that should never be specialized or overloaded, and implement the function template entirely in terms of a class template. |