KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
What is the C++ syntax for specializing a template function that's inside a template class? For example, consider that I have the following two classes and their usage. I would like to be able to provide specialized implementations of the method X::getAThing() for different types. E.g.: int, std::string, arbitrary pointer or class, etc. template <class c1> class X { public: template<typename returnT> returnT getAThing(std::string param); static std::string getName(); private: c1 theData; }; // This works ok... template <class c1> std::string X<c1>::getName() { return c1::getName(); } // This blows up with the error: // error: prototype for 'int X<c1>::getAThing(std::string)' does not match any in class 'X<c1>' template <class c1> template <typename returnT> int X<c1>::getAThing(std::string param) { return getIntThing(param); // Some function that crunches on param and returns an int. } // More specialized definitions of getAThing() for other types/classes go here... class Y { public: static std::string getName() { return "Y"; } }; int main(int argc, char* argv[]) { X<Y> tester; int anIntThing = tester.getAThing<int>(std::string("param")); cout << "Name: " << tester.getName() << endl; cout << "An int thing: " << anIntThing << endl; } I've been trying to guess at the correct syntax for the specialization for at least an hour, and can't figure anything out that will compile. Any help would be greatly appreciated!
Tags (comma-separated)
Save Edits
Cancel