Immediate Functions in C++
In this article, we will discuss the immediate function used in C++.
- In C++20, an immediate function is a function where every call to the function either directly or indirectly produces a compile-time constant expression.
- These functions are declared by using a consteval keyword before their return type.
Below given some important terms related to Immediate Function:
- The constexpr specifier declares that it is possible to evaluate the value of a function or variable at compile time.
- Such variables and functions can then be used where only compile-time constant expressions are allowed.
- These functions are used to improve the performance of the program by doing computations at compile time instead of run time.
- These functions can really be helpful, where executing a program multiple times as the constant expressions will only be evaluated once during the compile time.
Below is the C++ program illustrating the use of constexpr function:
The fibonacci number is: 17711
- In the above example, fib() is called with 22.
- That’s why, it is a constant expression and so, it can be evaluated at compile time.
- The program shows no error either at using constexpr or consteval.
- But, if the constant expression is used while using the consteval keyword, the program will produce an error.
- In consteval function, every call to the function must directly or indirectly produce a compile-time constant expression.
- The consteval function is the same as constexpr function except that if the call to a consteval function doesn’t evaluate to a compile-time constant expression, then the program gives an error while it is not so in the case of a constexpr function.
- The constexpr specifies that the value of a variable or function can appear in constant expressions.
- The key to note here is that it says, a function can appear in constant expressions, it doesn’t say that the function has to be, while a consteval specifies that a function is an immediate function, that is, every call to the function must produce a compile-time constant.
Below is the C++ program illustrating the use of consteval function:
The fibonacci number is: 2
- In the above example, rand() is used and rand() gets evaluated at runtime not at compile time and so because of that, the expression is no longer a constant expression, that’s why our consteval function will now produce an error.
- While the constexpr function still works fine and the reason is that it doesn’t have to be at compile time.
- From the above discussion, it can be concluded that the Immediate function is consteval function, which works fine only when every call to the function must directly or indirectly produce a compile-time constant expression otherwise gives an error.
- These functions are declared by using a consteval keyword before their return type and are used to reduce the time, consumed in evaluating the constant expressions as they evaluate the constant expressions only once during the compile-time and not during every run/execution of the program.
- Hence, it saves a considerable amount of time when there is a need to execute a program with some constant expressions multiple times.