title: Is constexpr really needed?
body: <p>I have been looking at the new <code>constexpr</code> feature of C++ and I do not fully understand the need for it.</p>
  
  <p>For example, the following code:</p>
  
  <pre><code>constexpr int MaxSize()
  {
      ...
  
      return ...;
  }
  
  void foo()
  {
      int vec[MaxSize()];
  }
  </code></pre>
  
  <p>can be replaced by:</p>
  
  <pre><code>int MaxSize()
  {
      ...
  
      return ...;
  }
  
  static const int s_maxSize = MaxSize();
  
  foo()
  {
      int vec[s_maxSize];
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>The second example is actually not standard ISO C++ (thanks to several users for pointing this out) but certain compilers (e.g. gcc) support it. So it is not <code>const</code> that makes the program valid, but the fact that gcc supports this non-standard feature. (To my knowledge, this is possible only when the array is defined as local to a function or method, since the size of a global array must still be known at compile time.) If I compile without the options <code>-std=c++98 -pedantic-errors</code>, even the code</p>
  
  <pre><code>int MaxSize()
  {
      return 10;
  }
  
  void foo()
  {
      int vec[MaxSize()];
  }
  </code></pre>
  
  <p>will compile with gcc.</p>
  
  <p>So I will try to rephrase my question taking into account the feedback that came so far (and also some further reading I have done in the mean time).</p>
  
  <p>I use the <code>const</code> keyword heavily. With <code>const</code> I can define a constant that has a certain value during its whole lifetime. A constant can be initialized with any expression, which is evaluated once, namely when the constant is created. For these cases, I think that <code>constexpr</code> is pretty useless: it would introduce a very small optimization in that the expression defining the constant value would be computed at compile time instead of run time. Every time I need a run-time constant with a complex initialization I use the keyword <code>const</code>.</p>
  
  <p>So <code>constexpr</code> may come in handy in situations where we need to initialize a constant at compile time. One example is a vector definition: the standard does not support defining the size at runtime. Another example is a template with one or more non-type parameters.</p>
  
  <p>In such cases I normally use macros:</p>
  
  <pre><code>#define MAX_SIZE (10)
  
  void foo()
  {
      int vec[MAX_SIZE];
  }
  </code></pre>
  
  <p>but, if I understand correctly, <code>constexpr</code> functions are more powerful than macros, since they allow recursive calls of <code>constexpr</code> functions in their definition. However, I cannot think of any practical application in which I ever wanted to use such a complex computation to define a compile-time constant.</p>
  
  <p>So, even if it may be an interesting feature, I still wonder if it is needed (i.e. how often it can solve situations where macros are not enough). Maybe looking at a few real-life examples that cannot be solved with macros would help me to change this opinion.</p>