Skip to content
Related Articles

Related Articles

Immediate Functions in C++
  • Last Updated : 04 May, 2021

In this article, we will discuss the immediate function used in C++.

Immediate Function:

  • 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:

constexpr 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:

C++14




// C++ program demonstrates the use of
// constexpr
#include <iostream>
using namespace std;
  
// Constexpr function if replaced with
// consteval, program works fine
constexpr int fib(int n)
{
    // Base Case
    if (n <= 1)
        return n;
  
    // Find the Fibonacci Number
    return fib(n - 1) + fib(n - 2);
}
  
// Driver Code
int main()
{
    // Constant expression evaluated
    // at compile time
    const int val = fib(22);
  
    cout << "The fibonacci number "
         << "is: " << val << "\n";
  
    return 0;
}


Output



The fibonacci number is: 17711

Explanation:

  • 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.

consteval function:

  • 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:

C++14




// C++ program illustrating the use
// of the consteval function
  
#include <iostream>
using namespace std;
  
// If constexpr replaces with consteval
// program gives an error in C++20
constexpr int fib(int n)
{
    // Base Case
    if (n <= 1)
        return n;
  
    return fib(n - 1) + fib(n - 2);
}
  
// Driver Code
int main()
{
    // This expression is not evaluated
    // at compile time
    const int val = fib(rand() % 20);
  
    cout << "The fibonacci number is: "
         << val << "\n";
  
    return 0;
}


Output

The fibonacci number is: 2

Explanation:

  • 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.

Conclusion:

  • 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.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :