title: CRTP: How to infer type of member to be used as return type?
body: <p>I would like to make the return type of a CRTP base method depend on the type of a member in the derived, as for example in:</p>
  
  <pre><code>template &lt;typename C&gt;
  struct sum_a_b {
      ??? sum() { return static_cast&lt;C*&gt;(this)-&gt;a + static_cast&lt;C*&gt;(this)-&gt;b; }
  }
  
  template &lt;typename T&gt; struct a_b : sum_a_b&lt;a_b&lt;T&gt;&gt; { T a,b; };
  </code></pre>
  
  <p><strong>What should I put in place of <code>???</code></strong></p>
  
  <p>I tried different ways to declare the return type : </p>
  
  <pre><code>template &lt;typename T&gt;
  struct base {
      int get_ok() {  
          return static_cast&lt;T*&gt;(this)-&gt;value; 
      }
      auto get_invalid() -&gt; decltype(static_cast&lt;T*&gt;(this)-&gt;value) {
          return static_cast&lt;T*&gt;(this)-&gt;value; 
      }
      typename T::value_type get_incomplete_type_foo() {  
          return static_cast&lt;T*&gt;(this)-&gt;value; 
      }
      auto get_incomplete_type_again() -&gt; decltype(T().value) {  
          return static_cast&lt;T*&gt;(this)-&gt;value; 
      }
  };
  
  struct foo : base&lt;foo&gt; {
          typedef int value_type;
          value_type value;
  };
  </code></pre>
  
  <p>The only methods that compiles is <code>int get_ok</code>, for the others I get either (for <code>get_invalid_cast</code>):</p>
  
  <pre><code>invalid static_cast from type 'base&lt;foo&gt;*' to type 'foo*'
       auto get_invalid() -&gt; decltype(static_cast&lt;T*&gt;(this)-&gt;value) {  return static_cast&lt;T*&gt;(this)-&gt;value; }
                                      ^
  </code></pre>
  
  <p>or (the other two)</p>
  
  <pre><code>invalid use of incomplete type 'struct foo'
       typename T::value_type get_incomplete_type_foo() {  return static_cast&lt;T*&gt;(this)-&gt;value; }
                              ^
  </code></pre>