title: Inferring return type of templated member functions in CRTP
body: <p><strong>Is it possible to infer the return type of a templated member function in a CRTP base class?</strong></p>
  
  <p>While inferring argument types works well, it fails with the return type. Consider the example below.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template &lt;typename Derived&gt;
  struct base
  {
    template &lt;typename R, typename T&gt;
    R f(T x)
    {
      return static_cast&lt;Derived&amp;&gt;(*this).f_impl(x);
    }
  };
  
  struct derived : base&lt;derived&gt;
  {
    bool f_impl(int x)
    {
      std::cout &lt;&lt; "f(" &lt;&lt; x &lt;&lt; ")" &lt;&lt; std::endl;
      return true;
    }
  };
  
  int main()
  {
    bool b = derived{}.f(42);
    return b ? 0 : 1;
  }
  </code></pre>
  
  <p>This produces the following error:</p>
  
  <pre><code>  bool b = derived{}.f(42);
             ~~~~~~~~~~^
  crtp.cc:7:5: note: candidate template ignored: couldn't infer template argument 'R'
    R f(T x)
      ^
  1 error generated.
  </code></pre>
  
  <p>My intuitive assumption is that if the compiler is capable of inferring type <code>int</code> for the argument to <code>f</code>, it should also work for the return <code>bool</code>, because both types are known at template instantiation time.</p>
  
  <p>I tried using the trailing return type function syntax, but then failed to find a working expression to put in <code>decltype</code>.</p>
  
  <h2>EDIT 1</h2>
  
  <p>For the case where the function has one or more templated arguments, Dietmar Kühl provided a solution based on delaying template instantiation using on layer of indirection. Unfotunately, this does not work when a base class function does not have any argument, like this:</p>
  
  <pre><code>template &lt;typename R&gt;
  R g()
  {
    return static_cast&lt;Derived&amp;&gt;(*this).g_impl();
  }
  </code></pre>
  
  <p>Attempts to use the same technique fail because there exist no dependent types. How does one handle this case?</p>
  
  <h2>EDIT 2</h2>
  
  <p>As pointed out by Johannes Schaub, C++11 features default template arguments, so it is always possible to make <code>g</code> dependent on an arbitrary type and then apply Dietmar's solution:</p>
  
  <pre><code>template &lt;typename T = void&gt;
  auto g() -&gt; typename g_impl_result&lt;Derived, T&gt;::type
  {
    return static_cast&lt;Derived&amp;&gt;(*this).g_impl();
  }
  </code></pre>
  
  <h2>EDIT 3</h2>
  
  <p>This problem does not exist in C++14 anymore, since we have return type deduction for normal functions, allowing us to write simply:</p>
  
  <pre><code>template &lt;typename Derived&gt;
  struct base
  {
    template &lt;typename T&gt;
    auto f(T x)
    {
      return static_cast&lt;Derived&amp;&gt;(*this).f_impl(x);
    }
  
    auto g()
    {
      return static_cast&lt;Derived&amp;&gt;(*this).g_impl();
    }
  };
  
  struct derived : base&lt;derived&gt;
  {
    bool f_impl(int x)
    {
      return true;
    }
  
    double g_impl()
    {
      return 4.2;
    }
  };
  </code></pre>