title: enum vs constexpr for actual static constants inside classes
body: <p>Let me start by stating my intent. In the olden (C++) days, we would have code like:</p>
  
  <pre><code>class C
  {
  public:
    enum {SOME_VALUE=27};
  };
  </code></pre>
  
  <p>Then we could use <code>SOME_VALUE</code> throughout our code as a compile time constant and wherever the compiler would see <code>C::SOME_VALUE</code>, it would just insert the literal 27.</p>
  
  <p>Now days, it is seems more acceptable to change that code to something like:</p>
  
  <pre><code>class C
  {
  public:
    static constexpr int SOME_VALUE=27;
  };
  </code></pre>
  
  <p>This looks much cleaner, gives <code>SOME_VALUE</code> a well defined type and seems to be the preferred approach as of C++11. The (unforseen at least for me) problem is that this also causes scenarios where <code>SOME_VALUE</code> needs to be made external. That is, in some cpp file somewhere, we need to add:</p>
  
  <pre><code>constexpr int C::SOME_VALUE; // Now C::SOME_VALUE has external linkage
  </code></pre>
  
  <p>The cases that cause this seem to be when const references to <code>SOME_VALUE</code> are used, which happens quite often in C++ Standard Library code (See the example at the bottom of this question). I am using gcc 4.7.2 as my compiler by the way. </p>
  
  <p>Due to this dilemma, I am forced to revert back to defining <code>SOME_VALUE</code> as an enum (i.e., old school) in order to avoid having to add a definition to a cpp file for some, but not all of my static constexpr member variables. Isn't there some way to tell the compiler that <code>constexpr int SOME_VALUE=27</code> means that <code>SOME_VALUE</code> should be treated <em>only</em> as a compile time constant and never an object with external linkage? If you see a const reference used with it, create a temporary. If you see its address taken, generate a compile time error if that's what's needed, because it's a compile time constant and nothing more.</p>
  
  <p>Here is some seemingly benign sample code that causes us to need to add the definition for <code>SOME_VALUE</code> in a cpp file (once again, tested with gcc 4.7.2):</p>
  
  <pre><code>#include &lt;vector&gt;
  
  class C
  {
  public:
    static constexpr int SOME_VALUE=5;
  };
  
  int main()
  {
    std::vector&lt;int&gt; iv;
  
    iv.push_back(C::SOME_VALUE); // Will cause an undefined reference error
                                 // at link time, because the compiler isn't smart
                                 // enough to treat C::SOME_VALUE as the literal 5
                                 // even though it's obvious at compile time
  }
  </code></pre>
  
  <p>Adding the following line to the code at file scope will resolve the error:</p>
  
  <pre><code>constexpr int C::SOME_VALUE;
  </code></pre>