title: How to avoid repetition of the enclosing class type when auto and decltype() cannot be used
body: <p>I recently discovered the <code>auto</code> and <code>decltype()</code> features of C++11, which are excellent, as they allow eliminating a lot of redundant type code. However, there are contexts where they cannot be used. One example I'm primarily asking about is if you want to declare a variable whose type uses the enclosing class type, either directly, or as a template argument, and you don't have an initialization expression (which would have allowed you to use <code>auto</code>). This is particularly undesirable if the enclosing class type is a template class with many template parameters. For example:</p>
  
  <pre><code>template&lt;typename T1, typename T2, typename T3, typename T4, typename T5 &gt; struct S {
      std::map&lt;int,S&lt;T1,T2,T3,T4,T5&gt;*&gt; m1;
      std::map&lt;int,S&lt;T1,T2,T3,T4,T5&gt;*&gt; m2;
      std::map&lt;int,S&lt;T1,T2,T3,T4,T5&gt;*&gt; m3;
      std::map&lt;int,S&lt;T1,T2,T3,T4,T5&gt;*&gt; m4;
      std::map&lt;int,S&lt;T1,T2,T3,T4,T5&gt;*&gt; m5;
  };
  </code></pre>
  
  <p>A logical solution is to use a typedef:</p>
  
  <pre><code>template&lt;typename T1, typename T2, typename T3, typename T4, typename T5 &gt; struct S {
      typedef S&lt;T1,T2,T3,T4,T5&gt; ThisClass;
      std::map&lt;int,ThisClass*&gt; m1;
      std::map&lt;int,ThisClass*&gt; m2;
      std::map&lt;int,ThisClass*&gt; m3;
      std::map&lt;int,ThisClass*&gt; m4;
      std::map&lt;int,ThisClass*&gt; m5;
  };
  </code></pre>
  
  <p>But it's still undesirable to have to declare a typedef that just repeats the enclosing class's type.</p>
  
  <p>This can actually be solved if you're inside an instance method, by deducing the type of <code>*this</code>, although the necessary code is more verbose than I would like:</p>
  
  <pre><code>auto copy(void) {
      typename std::remove_reference&lt;decltype(*this)&gt;::type s = *this;
      // ... do stuff with s ...
      return s;
  }
  </code></pre>
  
  <p>This solution does not work in class scope, because <code>this</code> is not allowed and is not meaningful outside of instance methods (the compiler complains <code>"invalid use of ‘this’ at top level"</code>).</p>
  
  <p>So, my question is, when you can't use <code>auto</code> or <code>decltype()</code>, what is the recommended solution for avoiding repetition of the enclosing class's type when you have to use it inside the class definition? Is a typedef the only option?</p>