You cannot partially specialize a function if you declare
template<class T> T my_function() { .... } template<class T> std::list<T> my_function() { .... }
and try calling the first with
my_function<int>();
since partial specializations are not allowed for functions, these declarations will be contradictory (in fact, these are two different declarations, and what's worse: they both correspond to this instance).
What you can do is wrap your function in a class or structure that can handle partial specializations for it:
#include <iostream> #include <list> using namespace std; template<class T> struct funWrapper { T my_function() { cout << "normal" << endl; return 0; } }; template<class T> struct funWrapper<std::list<T>> { std::list<T> my_function() { cout << "stdlist"; return std::list<T>(); } }; int main() { funWrapper<int> obj; obj.my_function(); funWrapper<std::list<int>> obj2; obj2.my_function(); return 0; }
http://ideone.com/oIC2Hf
Marco A.
source share