title: Passing literal as a const ref parameter
body: <p>Imagine the following simplified code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  void foo(const int&amp; x) { do_something_with(x); }
  
  int main() { foo(42); return 0; }
  </code></pre>
  
  <p>(1) Optimizations aside, what happens when 42 is passed to <code>foo</code>?</p>
  
  <p>Does the compiler stick 42 somewhere (on the stack?) and pass its address to <code>foo</code>?</p>
  
  <p>(1a) Is there anything in the standard that dictates what is to be done in this situation (or is it strictly up to the compiler)?</p>
  
  <hr>
  
  <p>Now, imagine slightly different code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  void foo(const int&amp; x) { do_something_with(x); }
  
  struct bar { static constexpr int baz = 42; };
  
  int main() { foo(bar::baz); return 0; }
  </code></pre>
  
  <p>It won't link, unless I define <code>int bar::baz;</code> (due to ODR?).</p>
  
  <p>(2) Besides ODR, why can't the compiler do whatever it did with 42 above?</p>
  
  <hr>
  
  <p>An obvious way to simplify things is to define <code>foo</code> as:</p>
  
  <pre><code>void foo(int x) { do_something_with(x); }
  </code></pre>
  
  <p>However, what would one do in case of a template? Eg:</p>
  
  <pre><code>template&lt;typename T&gt;
  void foo(T&amp;&amp; x) { do_something_with(std::forward&lt;T&gt;(x)); }
  </code></pre>
  
  <p>(3) Is there an elegant way to tell <code>foo</code> to accept <code>x</code> by value for primitive types? Or do I need to specialize it with SFINAE or some such?</p>
  
  <p><strong>EDIT</strong>: Modified what happens inside <code>foo</code> as it's irrelevant to this question.</p>