const — runtime constant. constexpr — compile-time constant.
1// const — runtime2const int x = 10;3int arr[x]; // Error in C++ (VLA not standard)45// constexpr — compile-time6constexpr int y = 10;7int arr2[y]; // OK!89// constexpr function10constexpr int square(int n) {11 return n * n;12}1314int arr3[square(5)]; // arr3[25]1516// const with functions17class MyClass {18public:19 int getValue() const { // const method20 return value;21 }2223private:24 int value;25};
Key differences: