constexpr is a feature added in C++ 11. The main idea is performance improvement of programs by doing computations at compile time rather than run time. Note that once a program is compiled and finalized be developer, it is run multiple times by users. The idea is to spend time in compilation and save time at run time (similar to template metaprogramming)
constexpr specifies that the value of an object or a function can be evaluated at compile time and the expression can be used in other constant expressions. For example, in below code product() is evaluated at compile time.
A function be declared as constexpr
- In C++ 11, a constexpr function should contain only one return statement. C++ 14 allows more than one statements.
- constexpr function should refer only constant global variables.
- constexpr function can call only other constexpr function not simple function.
- Function should not be of void type and some operator like prefix increment (++v) are not allowed in constexpr function.
constexpr vs inline functions:
Both are for performance improvements, inline functions are request to compiler to expand at compile time and save time of function call overheads. In inline functions, expressions are always evaluated at run time. constexpr is different, here expressions are evaluated at compile time.
Example of performance improvement by constexpr:
Consider the following C++ program
When the above program is run on GCC, it takes 0.003 seconds (We can measure time using time command)
If we remove const from below line, then the value of fib(5) is not evaluated at compile time, because result of constexpr is not used in a const expression.
Change, const long int res = fib(30); To, long int res = fib(30);
After making above change, time taken by program becomes higher 0.017 seconds.
constexpr with constructors:
constexpr can be used in constructors and objects also. See this for all restrictions on constructors that can use constexpr.
constexpr vs const
They serve different purposes. constexpr is mainly for optimization while const is for practically const objects like value of Pi.
Both of them can be applied to member methods. Member methods are made const to make sure that there are no accidental changes by the method. On the other hand, the idea of using constexpr is to compute expressions at compile time so that time can be saved when code is run.
const can only be used with non-static member function whereas constexpr 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.
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- C++ final specifier
- _Noreturn function specifier in C
- Using a variable as format specifier in C
- Difference between %d and %i format specifier in C language
- Understanding static_assert in C++ 11
- Understanding nullptr in C++
- Understanding "extern" keyword in C
- Understanding "register" keyword in C
- OpenCV | Understanding Brightness in an Image
- OpenCV | Understanding Contrast in an Image
- Understanding "volatile" qualifier in C | Set 1 (Introduction)
- Understanding "volatile" qualifier in C | Set 2 (Examples)
- Understanding Lvalues, PRvalues and Xvalues in C/C++ with Examples
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code