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.
varA
is a normal scenario whilevarB
&varC
will not take further value or assignment.varB
&varC
are fixed at compile time if we have defined them like above. - But,
varB
is 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
varB
would not anymore compile time. While statement withvarC
will throw compilation error. The reason is constexpr will always accept a strictly compile-time value.
constexpr Functions
constexpr
specifies 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
const
orconstexpr
, call to the functionsum
is 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 functionsum
at 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.
constexpr
is mainly for optimization whileconst
is for practicallyconst
objects like the value ofPi
. const
&constexpr
both can be applied to member methods. Member methods are madeconst
to make sure that there are no accidental changes by the method. On the other hand, the idea of usingconstexpr
is to compute expressions at compile time so that time can be saved when the code is running.const
can only be used with non-static member function whereasconstexpr
can 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
.