Javatpoint Logo
Javatpoint Logo

Constexpr in C++

In C++ 11, a feature called constexpr was included. The fundamental concept is to increase software performance by performing calculations at compile time rather than run time. It should be noted that consumers often run software numerous times after the developer has finished compiling and finalizing it. The objective is to invest time in compilation while saving time at runtime (similar to template Meta programming). Constexpr indicates that an object's or function's value may be determined at compile time and that the expression may be used in other constant expressions.

OUTPUT:

200
.........
Process executed in 1.11 seconds
Press any key to continue.

The const qualifier lets programmers and compilers know that we intend for a variable to be read-only and that any attempts to alter it will cause a compilation error.

In that it indicates a const, constexpr is equivalent to const. That, too, is immutable. Const is different because, depending on how we initialize it, it can be evaluated both at compile time and at run time.

Explanation

  • A constexpr function in C++ 11 should only have one return statement. In C++ 14, multiple statements are permitted.
  • Only constant global variables should be used with the constexpr function.
  • Only other constexpr functions, not simple functions, may be called by constexpr functions.
  • The function's type shouldn't be void.
  • Constexpr function prefix increment (++v) was not permitted in C++11; however, this limitation has been lifted in C++14.

Writing a function that just returns the multiplication of a given integer as constexpr can seem pointless. Where else may the usage of this feature be found outside performance enhancement?

  • The major benefit of this feature is that it enables us to assess compile-time constants using a function. Using this, we were able to compute the array's size at compilation time, which was previously not feasible.

OUTPUT:

6
……..
Process executed in 1.11 seconds
Press any key to continue.

Converting the device from one system to another is another realistic use case. For instance, although most people find it simpler to use angle in degrees, the trigonometric function in C/C++ takes angle in radians. So, we could define the ConvertDegreeToRadian() method as a constexpr without sacrificing code clarity or efficiency.

OUTPUT:

Angle in radian: 1.5708
………………………………….
Process executed in 0.11 seconds
Press any key to continue. 

Constexpr Vs inline functions

constexpr Inline functions
As the code and expressions are evaluated during compilation, the function calls are removed. It virtually ever eliminates any function calls since it acts on expressions at run time.
The value of the variable or function can be evaluated during compilation. The value of the function or variable cannot be determined at compile time.
It makes no inference about external links. It suggests an external link.

Constexpr performance improvement example:

// A C++ program to demonstrate the use of constexpr

OUTPUT:

832040
………….
Process executed in 0.003 seconds
Press any key to continue.

If we delete const from the below line, then the value of fib(5) is not evaluated at compile-time since the result of constexpr is not utilized in a const expression. The above program runs on GCC in 0.003 seconds (we can measure time using the time command).

After making the aforementioned modification, the program's execution time increases by 0.017 seconds.

Constructors and constexpr: Constructors that are declared with a constexpr specifier are considered constexpr constructors. Constructors and objects can also be created using constexpr. Constructor for a constexpr is implicitly inlined.

Constructor limitations for constexpr usage:

  • lacking a virtual base class
  • Every parameter needs to be literal.
  • The function is not a try-block one.

OUTPUT:

200
……..
Process executed in 0.17 seconds
Press any key to continue.

Constexpr Vs const

They have several functions. Const is for practically const things like the value of Pi, whereas constexpr is primarily for efficiency. Member methods can use either of these. To prevent unintentional modifications to the function, member methods are turned into const. contrarily, the purpose of constexpr is to compute expressions at build time, saving time when the code is executed. Constexpr may be used with member and non-member functions, including constructors, whereas const can only be used with non-static member functions. Nevertheless, there is a requirement that the argument and return types must be literal types.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA