title: Overwriting a Static Const Variable
body: <p>I have written a small method that is supposed to allow me to easily overwrite the value of a static constant variable.</p>
  
  <p>Here is the variable I want to change:</p>
  
  <pre><code>static const unsigned int myInt;
  </code></pre>
  
  <p>Here is the method that I am trying to use:</p>
  
  <pre><code>template&lt;typename T&gt;
  void MyClass::SetConstantVariableValue(void* destination, T&amp; value)
  {
      memcpy(destination, (const void*)&amp;value, sizeof(value));
  }
  </code></pre>
  
  <p>And here is the statement used to call the method:</p>
  
  <pre><code>int a = 5;
  this-&gt;SetConstantVariableValue((void*)&amp;myInt, a);
  </code></pre>
  
  <p>My problem is that it works perfectly if <b>myInt</b> is only static and not constant. As soon as I define it as constant, <b>memcpy</b> crashes from an access violation.</p>
  
  <pre><code>Unhandled exception at 0x596EEB90 (msvcr110d.dll)
  0xC0000005: Access violation writing location 0x00AC16B8.
  </code></pre>
  
  <p>It is my understanding that <b>memcpy</b> ignores the fact that a variable might be constant since it is performed at runtime and has no idea what the datatype is for either the destination or the source. If that is correct, then the combination of being <i>static</i> and <i>const</i> is what is causing the crash. I have not been able to find anything explaining as to why this might cause <b>memcpy</b> to crash. Anyone know what I am doing wrong?</p>