title: Can c++ 'pass by reference if possible'?
body: <p>I have a method with a parameter that is read-only.</p>
  
  <pre><code>template &lt;typename T&gt;
  void foo(const T t) {...}
  </code></pre>
  
  <p>This definition has the benefit that <code>foo(2)</code> is syntactically correct. However, it has the downside that applying <code>foo</code> on a big object makes it copy the object. Since I only read from the argument, this is just a waste of time and memory.</p>
  
  <p>The standard solution is to make the parameter <code>t</code> a const reference. However, this has the downside that <code>foo(2)</code> is not syntactically correct anymore. I would have to do <code>int i = 2; foo(i);</code> which I find unfortunate.</p>
  
  <p>Is there a way to get the best of both worlds? So such that <code>foo(2)</code> is syntactically correct and applying <code>foo</code> on an object makes it pass by reference?</p>
  
  <p>EDIT: Apparently a const reference is already the best of both worlds. Now I would like to know why this is. Why is <code>foo(2)</code> syntactically correct? How can <code>2</code> become a reference here?</p>