title: Preventing non-const lvalues from resolving to rvalue reference instead of const lvalue reference
body: <p>I'm having trouble overloading a function to take a value either by const reference or, if it is an rvalue, an rvalue reference. The problem is that my non-const lvalues are binding to the rvalue version of the function. I'm doing this in VC2010.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  template &lt;class T&gt;
  void foo(const T&amp; t)
  {cout &lt;&lt; "void foo(const T&amp;)" &lt;&lt; endl;}
  
  template &lt;class T&gt;
  void foo(T&amp;&amp; t)
  {cout &lt;&lt; "void foo(T&amp;&amp;)" &lt;&lt; endl;}
  
  int main()
  {
      vector&lt;int&gt; x;
      foo(x); // void foo(T&amp;&amp;) ?????
      foo(vector&lt;int&gt;()); // void foo(T&amp;&amp;)
  }
  </code></pre>
  
  <p>The priority seems to be to deduce foo(x) as</p>
  
  <pre><code>foo&lt; vector&lt;int&gt; &amp; &gt;(vector&lt;int&gt;&amp; &amp;&amp; t)
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>foo&lt; vector&lt;int&gt; &gt;(const vector&lt;int&gt;&amp; t)
  </code></pre>
  
  <p>I tried replacing the rvalue-reference version with</p>
  
  <pre><code>void foo(typename remove_reference&lt;T&gt;::type&amp;&amp; t)
  </code></pre>
  
  <p>but this only had the effect of causing everything to resolve to the const-lvalue reference version.</p>
  
  <p>How do I prevent this behaviour? And why is this the default anyway - it seems so dangerous given that rvalue-references are allowed to be modified, this leaves me with an unexpectedly modified local variable.</p>
  
  <p>EDIT: Just added non-template versions of the functions, and they work as expected. Making the function a template changes the overload resolution rules? That is .. really frustrating!</p>
  
  <pre><code>void bar(const vector&lt;int&gt;&amp; t)
  {cout &lt;&lt; "void bar(const vector&lt;int&gt;&amp;)" &lt;&lt; endl;}
  
  void bar(vector&lt;int&gt;&amp;&amp; t)
  {cout &lt;&lt; "void bar(vector&lt;int&gt;&amp;&amp;)" &lt;&lt; endl;}
  
  bar(x); // void bar(const vector&lt;int&gt;&amp;)
  bar(vector&lt;int&gt;()); // void bar(vector&lt;int&gt;&amp;&amp;)
  </code></pre>