title: Does this const initialization through const_cast have undefined behaviour?
body: <p>According to my small tests this code works. But, does it have undefined behaviour? Modifying the const object through the use of const_cast resulted in run-time access violations in my previous tests, but I can't remember how they were different. So, is there fundamentally something wrong here or not?</p>
  
  <pre><code>// test.h
  #pragma once
  #include &lt;boost/array.hpp&gt;
  
  typedef boost::array&lt;int,100000&gt; bigLut_t;
  extern const bigLut_t constBigLut;
  </code></pre>
  
  <hr>
  
  <pre><code>// test.cpp
  #include "test.h"
  
  bigLut_t&amp; initializeConstBigLut()
  {
      bigLut_t* pBigLut = const_cast&lt;bigLut_t*&gt;( &amp;constBigLut );
  
      for(int i = 0; i &lt; 100000; ++i) {
          pBigLut-&gt;at(i) = i;
      }
      return const_cast&lt;bigLut_t&amp;&gt;(constBigLut);
  }
  
  const bigLut_t constBigLut = initializeConstBigLut();
  </code></pre>
  
  <hr>
  
  <pre><code>// const_test.cpp
  #include &lt;iostream&gt;
  #include "test.h"
  
  void main()
  {
      for(int i = 0; i &lt; 100; ++i) {
          std::cout &lt;&lt; constBigLut[i] &lt;&lt; std::endl;
      }
      system("pause");
  }
  </code></pre>
  
  <p>(Notice that sizeof(bigLut_t) is too much to fit into the stack.)</p>
  
  <p>EDIT:
  I actually like the idea in ybungalobill's small comment best for a method of initializing these big objects:</p>
  
  <pre><code>// test.h
  #pragma once
  #include &lt;boost/array.hpp&gt;
  
  extern const struct BigLut : public boost::array&lt;int,100000&gt; {
      BigLut();
  } constBigLut;
  </code></pre>
  
  <hr>
  
  <pre><code>// test.cpp
  #include "test.h"
  
  const BigLut constBigLut;
  BigLut::BigLut()
  {
      for(int i = 0; i &lt; 100000; ++i) {
          this-&gt;at(i) = i;
      }
  }
  </code></pre>