title: Called object type 'auto' is not a function or function pointer
body: <p>I'm seeing a strange error from Clang (3.4 and 3.5) when I try to compile this C++14 code that uses a variable template of lambda type.</p>
  
  <p><a href="http://goo.gl/ESb0Mn" rel="nofollow">Here's the C++14 code as I'd like to write it.</a></p>
  
  <p><a href="http://goo.gl/qgFajl" rel="nofollow">Here's a second version</a>, with some of the library <code>_t</code> and <code>_v</code> stuff removed to make the compiler happy:</p>
  
  <pre><code>#include &lt;type_traits&gt;
  
  template &lt;class T&gt;
  class forward_if_wrapper {
  
      template &lt;class U, class Enable = typename std::enable_if&lt;std::is_lvalue_reference&lt;U&gt;::value&gt;::type&gt;
      static U forward(U&amp;&amp; u) {
          return u;
      }
  
      template &lt;class U, class Enable = typename std::enable_if&lt;!std::is_lvalue_reference&lt;U&gt;::value&gt;::type&gt;
      static U&amp;&amp; forward(U&amp;&amp; t) {
          return static_cast&lt;U&amp;&amp;&gt;(t);
      }
  };
  auto forward = [](auto&amp;&amp; t) { return forward_if_wrapper&lt;decltype(t)&gt;::forward(t); };
  template &lt;class T&gt; auto forward_if = [](auto&amp;&amp; u) { return forward_if_wrapper&lt;T&gt;::forward(u); };
  
  // --------
  
  #include &lt;stdio.h&gt;
  #include &lt;vector&gt;
  
  template&lt;class Elt&gt;
  void bar(Elt&amp;&amp; e) {
      printf("Called %s\n", __PRETTY_FUNCTION__);
  }
  
  template&lt;class Container&gt;
  void foo(Container&amp;&amp; c) {
      for (auto&amp;&amp; elt : c) {
          bar(forward_if&lt;Container&gt;(elt));
      }
  }
  
  int main() {
      std::vector&lt;int&gt; v = {1,2};
      foo(v);
      foo(std::move(v));
  }
  </code></pre>
  
  <p>The error I see from the second code with Clang is:</p>
  
  <pre><code>test.cc:34:13: error: called object type 'auto' is not a function or function pointer
          bar(forward_if&lt;Container&gt;(elt));
              ^~~~~~~~~~~~~~~~~~~~~~~
  test.cc:43:5: note: in instantiation of function template specialization 'foo&lt;std::__1::vector&lt;int, std::__1::allocator&lt;int&gt; &gt; &amp;&gt;' requested here
      foo(v);
      ^
  </code></pre>
  
  <p>I don't have access to any other compiler that supports C++14 generic lambdas, so Clang is all I've tested.</p>
  
  <p>Is this my bug or Clang's bug?</p>
  
  <p>I'm leaning toward the idea that it's a bug in Clang's template instantiation rules: Clang seems to be treating <code>auto</code> as a first-class citizen in the type world, instead of as an indicator to perform type deduction.</p>
  
  <pre><code>test.cc:34:14: error: invalid argument type 'auto' to unary expression
          bar((+forward_if&lt;Container&gt;)(elt));
               ^~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  
  <p>Again:</p>
  
  <pre><code>template&lt;class T&gt; T x{0};  // good
  template&lt;class T&gt; auto x = T{0};  // bad
  
  int main()
  {
      return x&lt;int&gt; + x&lt;long&gt;;
  }
  
  bad.cc:6:19: error: invalid operands to binary expression ('auto' and 'auto')
  </code></pre>