Что такое синтаксис С++ для специализации функции шаблона внутри класса шаблона? Например, учтите, что у меня есть следующие два класса и их использование. Я хотел бы иметь возможность предоставлять специализированные реализации метода X:: getAThing() для разных типов. Например: int, std::string, произвольный указатель или класс и т.д.
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;
}
Я пытался угадать правильный синтаксис специализации в течение по крайней мере часа и не могу понять, что будет компилироваться. Любая помощь будет принята с благодарностью!