title: Force constexpr to be evaluated at compile time
body: <pre><code>#include &lt;algorithm&gt;
  
  struct S
  {
      static constexpr int X = 10;
  };
  
  int main()
  {
      return std::min(S::X, 0);
  };
  </code></pre>
  
  <p>If <code>std::min</code> expects a <code>const int&amp;</code>, the compiler very likely would like to have the <code>S::X</code> also defined somewhere, i.e. the storage of <code>S::X</code> must exists.</p>
  
  <p>See <a href="https://stackoverflow.com/questions/8452952/c-linker-error-with-class-static-constexpr">here</a> or <a href="https://stackoverflow.com/questions/12785691/c-constexpr-at-compile-time">here</a>.</p>
  
  <p>Is there a way to <em>force</em> the compiler to evaluate my <code>constexpr</code> at compile time?</p>
  
  <p>The reason is:</p>
  
  <p>Initially, we had a problem in early initialization of static variables in the init priority. There was some <code>struct Type&lt;int&gt; { static int max; };</code>, and some global <code>static int x = Type&lt;int&gt;::max;</code>, and some other early code <code>other_init</code> used that <code>x</code>. When we updated GCC, suddenly we had <code>x == 0</code> in <code>other_init</code>.</p>
  
  <p>We thought that we could avoid the problem by using <code>constexpr</code>, so that it would always evaluate it at compile time.</p>
  
  <p>The only other way would be to use <code>struct Type&lt;int&gt; { static constexpr int max(); };</code> instead, i.e. letting it be a function.</p>