While introducing myself to Modern C++ & its new features introduced in C++11 & C++14, I have completely neglected this keyword constexpr. Initially, I was confused about when to use const vs constexpr in C++ & how this constexpr works & differ with const. So, I have studied this from different sources & here is the consolidation of it:
Primitive constexpr Variables
- All of the above variable having a value which is known at compile time.
varAis a normal scenario whilevarB&varCwill not take further value or assignment.varB&varCare fixed at compile time if we have defined them like above. - But,
varBis not the right way(in some situation) of declaring the constant value at compile time. For example, if I declare them as follows:
- Value of
varBwould not anymore compile time. While statement withvarCwill throw compilation error. The reason is constexpr will always accept a strictly compile-time value.
constexpr Functions
constexprspecifies that the value of an object, variable and a function can be evaluated strictly at compile-time. And an expression can use in other constant expressions.
| |
- If you observe above code, you can see that when you catch result as
constorconstexpr, call to the functionsumis not there in assembly rather compiler will execute that function by itself at compile time & substitute the result with function. - By specifying
constexpr, we suggest compiler to evaluate the functionsumat compile time.
constexpr Constructors
- Above code is simple & self-explanatory. If it isn’t to you, then play with it here.
const vs constexpr in C++
- They serve different purposes.
constexpris mainly for optimization whileconstis for practicallyconstobjects like the value ofPi. const&constexprboth can be applied to member methods. Member methods are madeconstto make sure that there are no accidental changes by the method. On the other hand, the idea of usingconstexpris to compute expressions at compile time so that time can be saved when the code is running.constcan only be used with non-static member function whereasconstexprcan be used with member and non-member functions, even with constructors but with condition that argument and return type must be of literal types. You read about more limitations here.
Where to Use What?
- Where you need a value not often & calculating it would be a bit complex, then that is the place you need constexpr. Otherwise, things are fine with an older buddy
const. For example, Fibonacci number, factorial, etc.
- Often programmer would suggest using constexpr instead of a macro.
- Sometimes you have an expression, that evaluated down to a constant, while maintaining good readability and allowing slightly more complex processing than just setting a constant to a number. For example:
Its a pretty simple choice there but it does mean that if you call max with constant values, it is explicitly calculated at compile time and not at runtime.
- Another good example is converting units like
Here you can use constexpr.