I’ve been working on making sure I understand the syntax for C++ templates, and I think I’m down to one final case. If I have a templated class, which has as a member a templated method (unrelated to the template parameter of the class), can I define that method outside the class definition? If so, what is the syntax?
If the templated method is defined inside the templated class definition, everything is fine. But for defining the method outside the class, I’ve tried lots of combinations of keywords and angle brackets and I always get compiler error (Visual Studio 2012).
Here’s the issue, boiled down:
template <typename T>
class TestClass
{
public:
// ctor
TestClass(T classtype) {m_classtype = classtype;}
// The declaration/definition below is fine.
template <typename U> void MethodOk(U param) {printf("Classtype size: %d. Method parameter size: %d\n", sizeof(m_classtype), sizeof(param));}
// The declaration below is fine, but how do I define the method?
template <typename U> void MethodProblem(U param); // What is the syntax for defining this outside the class definition?
private:
T m_classtype;
};
1 Answer