title: How do I modify data structures at compile-time in c++?
body: <p>First of all, I am new to compile-time code execution (not to C++, I have a few years of experience), so if I say anything stupid please let me know.</p>
  <p>I basically want to make the <a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow noreferrer">Sieve of Eratosthenes</a> algorithm run at compile-time for the first 1000000 numbers (in the code below you can see that I have only the first 1000 numbers, this is because 1000000 numbers would overflow the stack, and the heap is out of the question), and have the program start with an array containing <code>0</code> for primes and <code>1</code> for the rest. I have tried a few times using a <code>constexpr</code> function but I can't get it to work.</p>
  <p>This is what I've tried (it's clear it won't work, since <code>arr</code> isn't constant):</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;array&gt;
  
  constexpr std::array&lt;bool, 1000&gt; Generate()
  {
      std::array&lt;bool, 1000&gt; arr{};
      arr[0] = arr[1] = 1;
      for (int i = 2; i &lt; 1000; i++)
      {
          if (arr[i] == 0)
              for (int j = 2; i * j &lt; 1000; j++)
                  arr[i * j] = true;
      }
      return arr;
  }
  
  int main()
  {
      constexpr std::array&lt;bool, 1000&gt; arr = Generate();
  
      for (int i = 0; i &lt; 1000; i++)
          std::cout &lt;&lt; i &lt;&lt; ' ' &lt;&lt; arr[i] &lt;&lt; '\n';
  
      return 0;
  }
  </code></pre>
  <p>Which generates:</p>
  <blockquote>
  <p>...\main.cpp(19,41): error C2131: expression did not evaluate to a constant</p>
  </blockquote>
  <p>My questions are:</p>
  <ul>
  <li>How do I modify any data structure at compile-time? (actually, any data at all. examples I've seen simply returned simple things like a sum)</li>
  <li>How do I make it work for 1000000 numbers? Is it even possible?</li>
  </ul>
  <p>Also I know I can just precompute the entire thing and just have</p>
  <pre><code>arr = {1, 1, 0, 0, 1, 0, ...}
  </code></pre>
  <p>but my aim is to have the compiler do that, and to use this as proof of concept for compile-time code.</p>
  <p>Note: I am using VS2022, and I have set the language standard to c++20</p>