title: C++ - Separate declaration/definition for template function in template class
body: <p>I am aware that the syntax for declaring a template class method in a header and defining it in a source file goes as so:</p>
  <p>myclass.h</p>
  <pre><code>template &lt;typename T&gt;
  class MyClass {
    public:
      void method(T input);
    private:
      T privVar;
  };
  </code></pre>
  <p>myclass.cpp</p>
  <pre><code>template &lt;typename T&gt;
  void MyClass&lt;T&gt;::method(T input) {
      privVar = input;
  }
  </code></pre>
  <p>But, what if the method is also a template?
  I am adding methods to the <code>basic_string</code> class, and I want to know how to write the implementation for the functions.</p>
  <p>MyString.h</p>
  <pre><code>template &lt;class _Elem   = TCHAR,
            class _Traits = std::char_traits&lt;_Elem&gt;,
            class _Ax     = std::allocator&lt;_Elem&gt;&gt;
  class String
      : public std::basic_string&lt;_Elem, _Traits, _Ax&gt; {
    private:
      // Types for the conversion operators.
      typedef       _Elem* _StrTy;
      typedef const _Elem* _ConstStrTy;
  
      //...
  
    public:
          // Conversion operators so 'String' can easily be
          // assigned to a C-String without calling 'c_str()'.
      operator _StrTy() const {
          return const_cast&lt;_StrTy&gt;(this-&gt;c_str());
      }
  
      operator _ConstStrTy() const {
          return this-&gt;c_str();
      }
  
      // ... Constructors ...
  
      /*------------ Additional Methods ------------*/
  
      //! Converts a value of the given type to a string.
      template &lt;class _ValTy&gt; static String ConvertFrom(_ValTy val);
  
      //! Converts a string to the given type.
      template &lt;class _ValTy&gt; static _ValTy ConvertTo(const String&amp; str);
      template &lt;class _ValTy&gt; _ValTy ConvertTo(void) const;
  
      //! Checks if a string is empty or is whitespace.
      static bool IsNullOrSpace(const String&amp; str);
      bool IsNullOrSpace(void) const;
  
      //! Converts a string to all upper-case.
      static String ToUpper(String str);
      void ToUpper(void);
  
      // ...
  };
  </code></pre>
  <p>How could I implement <code>template &lt;class _ValTy&gt; static String ConvertFrom(_ValTy val);</code>? Because now not only do I need to specify the class template, but the function template too. I am betting the code I'm about to write isn't valid, but it should show what I am trying to accomplish:</p>
  <p>MyString.cpp</p>
  <pre><code>template &lt;class _Elem, class _Traits, class _Ax&gt;
  template &lt;class _ValTy&gt;
  String&lt;_Elem, _Traits, _Ax&gt; String&lt;_Elem, _Traits, _Ax&gt;::ConvertFrom(_ValTy val) {
      // Convert value to String and return it...
  }
  </code></pre>
  <p>I am not advanced at all with templates. Not only am I very doubtful that the above is valid, it seems cumbersome to write and not very readable. How would I go about implementing the template methods, and the static template methods which returns its own class type? Because I don't want to define them in the header.</p>