title: constexpr vs. static const: Which one to prefer?
body: <p>For <strong>defining compile-time constants</strong> of integral types like the following (at function and class scope), which syntax is best?</p>
  
  <pre><code>static const int kMagic = 64; // (1)
  constexpr int kMagic = 64;    // (2)
  </code></pre>
  
  <p><code>(1)</code> works also for C++98/03 compilers, instead <code>(2)</code> requires at least C++11. Are there any other differences between the two? Should one or the other be preferred in modern C++ code, and why?</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried this sample code with <a href="https://godbolt.org/g/PdWdg6" rel="noreferrer">Godbolt's CE</a>:</p>
  
  <pre><code>int main()
  {
  #define USE_STATIC_CONST
  #ifdef USE_STATIC_CONST
    static const int kOk = 0;
    static const int kError = 1;
  #else
    constexpr int kOk = 0;
    constexpr int kError = 1;
  #endif
    return kOk;
  }
  </code></pre>
  
  <p>and for the <code>static const</code> case this is the generated assembly by GCC 6.2:</p>
  
  <pre><code>main::kOk:
          .zero   4
  main::kError:
          .long   1
  main:
          push    rbp
          mov     rbp, rsp
          mov     eax, 0
          pop     rbp
          ret
  </code></pre>
  
  <p>On the other hand, for <code>constexpr</code> it's:</p>
  
  <pre><code>main:
          push    rbp
          mov     rbp, rsp
          mov     DWORD PTR [rbp-4], 0
          mov     DWORD PTR [rbp-8], 1
          mov     eax, 0
          pop     rbp
          ret
  </code></pre>
  
  <p>Although at <code>-O3</code> in both cases I get the same (optimized) assembly:</p>
  
  <pre><code>main:
          xor     eax, eax
          ret
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT #2</strong></p>
  
  <p>I tried this simple code <a href="https://ideone.com/WiXnua" rel="noreferrer"><strong>(live on Ideone)</strong></a>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main() {
      const int k1 = 10;
      constexpr int k2 = 2*k1;
      cout &lt;&lt; k2 &lt;&lt; '\n';
      return 0;
  }
  </code></pre>
  
  <p>which shows that <code>const int k1</code> is evaluated at <em>compile-time</em>, as it's used to calculate <code>constexpr int k2</code>.</p>
  
  <p>However, there seems to be a <em>different</em> behavior for <code>double</code>s. I've created a separate question for that <a href="https://stackoverflow.com/questions/41141734/initializing-constexpr-with-const-different-treatment-for-int-and-double">here</a>.</p>