Based on the tutorial ,
// Example 2: Explicit specialization
//
template<class T> // (a) a base template
void f( T ){;}
template<class T> // (b) a second base template, overloads (a)
void f( T* ){;} // (function templates can't be partially
// specialized; they overload instead)
template<> // (c) explicit specialization of (b)
void f<>(int*){;} // ===> Method one
I also test the following with VS2010 SP1 without any warning.
template<> // (c) alternative
void f<int>(int*){;} // ==> Method two
Question> Which way is recommended way based on the C++ standard? Method one or Method two?
As you can see the key different between Method one and Method two is as follows:
template<>
void f<>(int*){;} // ===> Method one
template<>
void f<int>(int*){;} // ===> Method two
^^^
Based on the tutorial, we should write the following plain old function instead:
void f(int*){;}
But that is NOT the question I am asking:)
Thank you
The full specialization declaration can omit explicit template arguments when template being specialized can be determined via argument deduction (using as argument types the parameter types provided in the declaration) and partial ordering.[from "C++ Templates" by Vandervoode, Josuttis]
This it the case in your example so you can write:
template<>
void f(int){;}
to specialize (a) and
template<>
void f(int*){;}
to specialize (b).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With