私はC++テンプレートの構文を確実に理解するように取り組んできましたが、最後の1つのケースにたどり着いたと思います。テンプレート化されたメソッド(クラスのテンプレートパラメーターとは無関係)をメンバーとして持つテンプレート化されたクラスがある場合、そのメソッドをクラス定義の外で定義できますか?もしそうなら、構文は何ですか?
テンプレート化されたメソッドがテンプレート化されたクラス定義内で定義されている場合、すべてが正常です。しかし、クラスの外部でメソッドを定義するために、キーワードと山括弧の組み合わせをたくさん試しましたが、常にコンパイラエラーが発生します(Visual Studio2012)。
要約すると、問題は次のとおりです。
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;
};