The problem with the explicitly specialized function template applies only if the function is also overloaded:
template <typename T> void foo (T*);
Without overload # 1, the code will work as expected. However, a function that starts without overloading may have overloads added while saving code in the future.
This is one of those examples where, although I don't want to say this, C ++ has too many ways to do the same. Personally, if you find that you need to specialize a function template, I like the template suggested by TimW in his comment against Neil Butterworth, i.e. This is best done if the current function sends a call to the template of a specialized class:
template <typename T> class DoFoo { static void do (T) { } }; template <> class DoFoo<int*> { static void do (int*) { } }; template <typename T> void foo (T t) { DoFoo<T>::do (t); }
If "foo" is overloaded, then at least it is more clear to the developer that this function will not be called, i.e. a developer does not need to be a standards guru to know how specialization rules interact with overload resolution.
Ultimately, however, the code generated by the compiler will be the same, it’s just a problem for the developer to understand the code.
Richard Corden
source share