title: Can I implement an autonomous `self` member type in C++?
body: <p>C++ <a href="https://stackoverflow.com/a/21143699/560648">lacks</a> the equivalent of <a href="http://www.php.net/manual/en/language.oop5.properties.php" rel="noreferrer">PHP's <code>self</code> keyword</a>, which evaluates to the type of the enclosing class.</p>
  
  <p>It's easy enough to fake it on a per-class basis:</p>
  
  <pre><code>struct Foo
  {
     typedef Foo self;
  };
  </code></pre>
  
  <p>but I had to write <code>Foo</code> again. Maybe I'll get this wrong one day and cause a silent bug.</p>
  
  <p>Can I use some combination of <code>decltype</code> and friends to make this work "autonomously"? <a href="http://coliru.stacked-crooked.com/a/583a8651b5f8a46c" rel="noreferrer">I tried the following already</a> but <code>this</code> is not valid in that place:</p>
  
  <pre><code>struct Foo
  {
     typedef decltype(*this) self;
  };
  
  // main.cpp:3:22: error: invalid use of 'this' at top level
  //     typedef decltype(*this) self;
  </code></pre>
  
  <p><sup>(I'm not going to worry about the equivalent of <code>static</code>, which does the same but with late binding.)</sup></p>