title: Modifying scoped enum by reference
body: <p>I am increasingly finding scoped enums unwieldy to use. I am trying to write a set of function overloads including a template for scoped enums that sets/initializes a value by reference--something like this:</p>
  
  <pre><code>void set_value(int&amp; val);
  void set_value(double&amp; val);
  template &lt;typename ENUM&gt; set_value(ENUM&amp; val);
  </code></pre>
  
  <p>However, I don't quite see how to write the templated version of <code>set_value</code> without introducing multiple temporary values:</p>
  
  <pre><code>template &lt;typename ENUM&gt;
  set_value(ENUM&amp; val)
  {
    std::underlying_type_t&lt;ENUM&gt; raw_val;
    set_value(raw_val);    // Calls the appropriate "primitive" overload
    val = static_cast&lt;ENUM&gt;(raw_val);
  }
  </code></pre>
  
  <p>I believe the <code>static_cast</code> introduces a second temporary value in addition to <code>raw_val</code>. I suppose it's possible that one or both of these could be optimized away by the compiler, and in any case it shouldn't really make much difference in terms of performance since the <code>set_value</code> call will also generate temporary values (assuming it's not inlined), but this still seems inelegant. What I would <em>like</em> to do would be something like this:</p>
  
  <pre><code>template &lt;typename ENUM&gt;
  set_value(ENUM&amp; val)
  {
    set_value(static_cast&lt;std::underlying_type_t&lt;ENUM&gt;&amp;&gt;(val));
  }
  </code></pre>
  
  <p>... but this isn't valid (nor is the corresponding code using pointers directly instead of references) because scoped enums aren't related to their underlying primitives via inheritance.</p>
  
  <p>I could use <code>reinterpret_cast</code>, which, from some preliminary testing, appears to work (and I can't think of any reason why it wouldn't work), but that seems to be frowned upon in C++.</p>
  
  <p>Is there a "standard" way to do this?</p>