I know that the syntax for declaring a template class method in the header and its definition in the source file looks like this:
myclass.h
template <typename T> class MyClass { public: void method(T input); private: T privVar; };
myclass.cpp
template <typename T> void MyClass<T>::method(T input) { privVar = input; }
But what if the method is also a template? I am adding methods to the basic_string class, and I want to know how to write an implementation for functions.
Mystring.h
template <class _Elem = TCHAR, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>> class String : public std::basic_string<_Elem, _Traits, _Ax> { private:
How could I implement template <class _ValTy> static String ConvertFrom(_ValTy val); ? Because now I not only need to specify a class template, but also a function template. I am sure that the code I'm going to write is invalid, but should show that I'm trying to execute:
Mystring.cpp
template <class _Elem, class _Traits, class _Ax> template <class _ValTy> String<_Elem, _Traits, _Ax> String<_Elem, _Traits, _Ax>::ConvertFrom(_ValTy val) {
I do not advance at all with templates. Not only do I very much doubt that the foregoing really seems cumbersome to write and not very readable. How can I apply template methods and static template methods that return their own class type? Because I do not want to define them in the title.
source share