title: deduced conflicting types for parameter 'T' for universal reference
body: <p>I am testing universal reference with the following code,</p>
  
  <pre><code>template &lt;typename T&gt;
  vector&lt;T&gt; attach_(vector&lt;T&gt; xs, T&amp;&amp; x) {
    xs.push_back(std::forward&lt;T&gt;(x));
    return xs;
  }
  
  int main() {
     int k = 2;
     attach_(std::move(vector&lt;int&gt;{1,2,3}),k);          //not OK
     attach_(std::move(vector&lt;int&gt;{1,2,3}),(int&amp;)k);    //not OK
     attach_(std::move(vector&lt;int&gt;{1,2,3}),(int)k);     //OK
     attach_(std::move(vector&lt;int&gt;{1,2,3}),2);          //OK
  }
  </code></pre>
  
  <p>and got an error:</p>
  
  <pre><code>no matching function for call to 'attach_(std::remove_reference&lt;std::vector&lt;int&gt; &gt;::type, int&amp;)'
  attach_(std::move(vector&lt;int&gt;{1,2,3}),k);
  note:   template argument deduction/substitution failed:
  note:   deduced conflicting types for parameter 'T' ('int' and 'int&amp;')
     attach_(std::move(vector&lt;int&gt;{1,2,3}),k);
  </code></pre>
  
  <p>SO has a question for a similar error <a href="https://stackoverflow.com/questions/21926678/error-message-deduced-conflicting-types-for-parameter-const-t">Error message &quot;deduced conflicting types for parameter &#39;const T&#39;&quot;</a> about const references. </p>
  
  <p>I also tested a few other cases, some with type conversions. Some work and others don't.</p>
  
  <p>I heard that universal references like <code>T&amp;&amp;</code> matches everything. Why does it fail here?</p>
  
  <p>A second question is, how to type <code>attach_</code> to make sure that move semantics work both for <code>xs</code> and <code>x</code> for the appropriate input? Ultimately, I'd like to have variant of the following:</p>
  
  <pre><code>for(int i = 0; i &lt; 100; i++)
     xs = attach_(xs,values[i])
  </code></pre>
  
  <p>work without making unnecessary copies.</p>
  
  <p>(This is tested with gcc4.8.1, using g++ -std=c++11 test.cpp)</p>
  
  <p>Thanks</p>
  
  <p>-- EDIT ---</p>
  
  <p>Thanks everyone for the excellent answers.</p>
  
  <p>So I understand now that for this case, it's efficient just to use pass-by-value, and move the <code>T</code>. It take it that the vector xs is not copied unnecessarily in parameter-passing and returning back, if used in a loop, right?</p>
  
  <p>I asked a related question <a href="https://stackoverflow.com/questions/24543330/when-is-const-reference-better-than-pass-by-value-in-c11">When is a const reference better than pass-by-value in C++11?</a>. There, I had this example where everyone said pass-by-vale is a bad idea:</p>
  
  <pre><code>int hd(vector&lt;int&gt; a) {
     return a[0];
  }
  </code></pre>
  
  <p>Is it possible at all to use universal reference to handle both the <code>hd</code> case and the <code>attach_</code> case in this post to avoid unnecessary copies?</p>
  
  <p>Thanks again.</p>
  
  <p>--- EDIT2 ---</p>
  
  <p>So, I tested the versions in the answers plus a <code>const</code> reference version below. Optimization is not used to expose any potential issues. The <code>const</code> ref version is the worst as it forces copying. Everything else has the same speed if <code>std::move(a)</code> is used for the vector, except raw <code>push_call</code> calls are faster. I guess optimization can remove that difference. I guess the test (or maybe the int type) is not big enough to show the difference between <code>push_back(x)</code> and <code>push_back(std::move(x))</code></p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;chrono&gt;
  using namespace std;
  
  template &lt;class T&gt;
  vector&lt;T&gt; attach(vector&lt;T&gt; v, T x) {
    v.push_back(x);
    return v;
  }
  
  template &lt;typename T&gt;
  vector&lt;T&gt; attach1(vector&lt;T&gt; xs, T x) {
    xs.push_back(std::move(x));
    return xs;
  }
  
  template &lt;typename T, typename E = typename std::remove_reference&lt;T&gt;::type&gt;
  std::vector&lt;E&gt; attach2(std::vector&lt;E&gt; xs, T&amp;&amp; x) {
    xs.push_back(std::forward&lt;T&gt;(x));
    return xs;
  }
  
  template &lt;typename C, typename T&gt; C attach3(C&amp;&amp; xs, T&amp;&amp; x) {
    xs.push_back(std::move&lt;T&gt;(x));
    return std::forward&lt;C&gt;(xs);
  }
  
  template &lt;class T&gt;
  vector&lt;T&gt; attach4(const vector&lt;T&gt;&amp; v, T x) {
    vector&lt;T&gt; ret = v;
    ret.push_back(x);
    return std::move(ret);
  }
  
  using namespace std::chrono;
  int main() {
    int N = 100000;
    vector&lt;int&gt; a;
    auto time = high_resolution_clock::now();
    for (int i = 0; i &lt; N; i++) {
      //a.push_back(i);    //0s
      //a = attach(a,i);    //15s
      //a = attach(std::move(a),i);    //0.03s
      //a = attach2(std::move(a),i);   //0.03s
      a = attach3(std::move(a),i);   //0.03s
      //a = attach4(std::move(a),i);   //14.9s
    }
    cout &lt;&lt; duration_cast&lt;duration&lt;double&gt;&gt;(high_resolution_clock::now() - time).count() &lt;&lt; endl;
  
  }
  </code></pre>