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
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?
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 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:
OUTPUT: 200 …….. Process executed in 0.17 seconds Press any key to continue. Constexpr Vs constThey 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. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India