
Joseph W. answered 01/23/21
UCLA Math Tutor with diverse experience
The const and constexpr keywords are both used to declare immutable objects. The difference is that const is evaluated at runtime, whereas constexpr allows for an object to be computed at compile time. This further optimizes a program, allowing faster performance and less memory consumption.
You would use constexpr when the object is defined using a literal or constant expression. Further, you can use constexpr for functions and class constructors.
The idea is that if the value of an object is not dependent on any variables and can known before runtime, we can use constexpr for improved performance.