Well, in addition, I think it works to specify the function of a template in some situations.
#include <iostream> #include <vector> using namespace std; class Base { public: virtual int test() {return 0;} }; class Derived : public Base { public: virtual int test() {return 1;} }; template<class T> void TestOutput(T* a) { cout << a->test() << endl; } template<> void TestOutput(Derived* a) { cout << "something else" << endl; TestOutput<Base>(a); } int main() { Derived d; TestOutput(&d); }
I compiled it with visual studio 2013, and the output is:
something else 1
Although I don't think you can always find the TestOutput function for Base to call the generic one.
source share