title: C++14: deduced (auto) return types from constexpr with ternary expressions
body: <p>I am experimenting with constexpr functions in C++14. The following code, which computes the factorial works as expected:</p>
  
  <pre><code>template &lt;typename T&gt;
  constexpr auto fact(T a) {
      if(a==1)
          return 1;
      return a*fact(a-1);
  }
  
  int main(void) {
      static_assert(fact(3)==6,  "fact doesn't work");
  }
  </code></pre>
  
  <p>when it is compiled as follows with clang:</p>
  
  <pre><code>&gt; clang++ --version
  clang version 3.5.0 (tags/RELEASE_350/final)
  Target: x86_64-unknown-linux-gnu
  Thread model: posix
  &gt; clang++ -std=c++14 constexpr.cpp
  </code></pre>
  
  <p>However, when I change the <code>fact</code> definition to use the ternary <code>?</code> operator:</p>
  
  <pre><code>template &lt;typename T&gt;
  constexpr auto fact(T a) {
      return a==1 ? 1 : a*fact(a-1);
  }
  </code></pre>
  
  <p>I get the following compiler error:</p>
  
  <pre><code>&gt; clang++ -std=c++14 constexpr.cpp
  constexpr.cpp:12:31: fatal error: recursive template instantiation exceeded maximum depth of
        256
      return a==T(1) ? T(1) : a*fact(a-1);
          ... snip ...
  constexpr.cpp:16:19: note: in instantiation of function template specialization 'fact&lt;int&gt;'
        requested here
      static_assert(fact(3)==6,  "fact doesn't work");
  </code></pre>
  
  <p>The problem is fixed if I explicitly state the return type T (instead of using auto to deduce the return type)</p>
  
  <pre><code>template &lt;typename T&gt;
  constexpr T fact(T a) {
      return a==1 ? 1 : a*fact(a-1);
  }
  </code></pre>
  
  <p>If I remove the template parameter, the pattern is repeated (the ternary version fails, and the <code>if</code> version works)</p>
  
  <pre><code>// this works just fine
  constexpr auto fact(int a) {
      if(a==1)
          return 1;
      return a*fact(a-1);
  }
  </code></pre>
  
  <p>whereas this fails</p>
  
  <pre><code>constexpr auto fact(int a) {
      return a==1 ? 1 : a*fact(a-1);
  }
  </code></pre>
  
  <p>with the following error</p>
  
  <pre><code>&gt; clang++ -std=c++14 constexpr.cpp
  constexpr.cpp:16:25: error: function 'fact' with deduced return type cannot be used before it
        is defined
      return a==1 ? 1 : a*fact(a-1);
                          ^
  constexpr.cpp:15:16: note: 'fact' declared here
  constexpr auto fact(int a) {
                 ^
  constexpr.cpp:20:26: error: invalid operands to binary expression ('void' and 'int')
      static_assert(fact(3)==6,  "fact doesn't work");
                    ~~~~~~~^ ~
  2 errors generated.
  </code></pre>
  
  <p>What is going on here?</p>