title: Why would you use a constexpr on a constructor?
body: <p>I understand that <code>constexpr</code> would allow you to be able to use an object as a constant at compile time, but what is an example of when this would be beneficial? I'm trying to understand the keyword better, but I can't find a good example that goes over when using it on a constructor that explains why it's needed.</p>
  
  <p>Both of the examples below work, so then why is the constexpr placed on the constructor?</p>
  
  <p><strong>With constexpr on constructor:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Rect
  {
      public:
          constexpr Rect(int width, int height)
              : mWidth(width), mHeight(height) {}
          constexpr int getArea() const { return mWidth * mHeight; }
      private:
          int mWidth, mHeight;
  };
  
  int main(int argc, char* argv[])
  {
      constexpr Rect r(8, 2);
  
      cout &lt;&lt; r.getArea() &lt;&lt; endl;   //16
  
      int myArray[r.getArea()];    // OK
  
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Without constexpr on constructor:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Rect
  {
      public:
          Rect(int width, int height)
              : mWidth(width), mHeight(height) {}
          constexpr int getArea() const { return mWidth * mHeight; }
      private:
          int mWidth, mHeight;
  };
  
  int main(int argc, char* argv[])
  {
      Rect r(8, 2);
  
      cout &lt;&lt; r.getArea() &lt;&lt; endl;   //16
  
      int myArray[r.getArea()];    // OK
  
  
      return 0;
  }
  </code></pre>