In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
For this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Clear11 has made some improvements to improve the efficiency of code execution. One of these improvements is to generate constant expressions that allow programs to take advantage of compile-time computing power. If you are familiar with template metaprogramming, you will find that constexpr makes it easier. Constexpr makes it easy for us to take advantage of upper compile-time programming.
Constant expressions mainly allow some calculations to occur at compile time, that is, when the code is compiled rather than run. This is a big optimization: if something can be done at compile time, it will be done only once, not every time the program runs. Need to calculate a constant that is known at compile time, such as sine or cosin of a specific value? It's true that you can also use library functions sin or cos, but then you have to spend runtime overhead. With constexpr, you can create a compile-time function that will calculate the values you need for you, and the user's computer will not need to do this work.
A preliminary study of 1.constexpr
In order for the function to gain the ability to calculate at compile time, you must specify the constexpr keyword to this function.
Constexpr int multiply (int x, int y) {return x * y;} / will calculate const int val = multiply (10,10) at compile time
In addition to the performance optimization of compile-time computing, another advantage of constexpr is that it allows functions to be applied to all previous calls to macros.
For example, you want a function that calculates the array size, where size is a multiple of 10. If you don't use constexpr, you need to create a macro or use a template, because you can't declare the size of the array with the return value of the function. But with constexpr, you can call a constexpr function to declare an array.
Constexpr int getDefaultArraySize (int multiplier) {return 10 * multiplier;} int my_array [getDefaultArraySize (3)]; int a = 4; / / non-constant expression getDefaultArraySize (a); / / ok
Constexpr-decorated function, simply put, if its passed parameters can be calculated at compile time, then this function will produce a compile-time value. However, if the parameters passed in cannot be calculated at compile time, then the constexpr-decorated function will be the same as the normal function. However, we don't have to write two versions of this, so if the body of the function applies to the conditions of the constexpr function, we can add constexpr as much as possible.
Limitations of 2.constexpr decorating functions
A constexpr has some strict requirements that must be followed:
There can be only one return statement in a function (but it is allowed to include typedefs, using declaration & & directives, static assertions, etc.)
Only other constexpr functions can be called
You can only use global constexpr variables
Note that recursion is not restricted, but only one return statement is allowed, so how do you implement recursion? You can use the triple operator (?:). For example, calculate the factorial of n:
Constexpr int factorial (int n) {return n > 0? N * factorial (n-1): 1;}
Now you can use factorial (2), and the compiler will calculate this value at compile time, which runs a more ingenious calculation, as opposed to inlining. You can't inline a recursive function.
3. Using compile-time objects
Constexpr modifies the constructor of the class, that is, to ensure that if all the arguments supplied to the constructor are constexpr, then all members of the resulting object will be constexpr, that is, the constexpr object, which can be used in various situations where only constexpr can be used. Note that the constexpr constructor must have an empty function body, that is, the initialization of all member variables is placed in the initialization list.
Suppose you have a Circle class:
Class Circle {public: constexpr Circle (int x, int y, int radius): _ x (x), _ y (y), _ radius (radius) {} constexpr double getArea () {return _ radius * _ radius * 3.1415926;} private: int _ x; int _ y; int _ radius;}
Declare the constructor and getArea as constexpr, so that during compilation, you can construct an object and call the getArea function to get area:
Constexpr Circle c (0,0,10); constexpr double area = c.getArea (); the difference between 4.constexpr vs const
If you mark a member function as constexpr, mark it as const by the way. If you mark a variable as constexpr, it is also const. But the opposite is not true. A variable or function of const is not constexpr.
Semantically:
Constexpr: tell the compiler that I can be known during compilation, so optimize me as much as you want.
Const: tell programmers that no one can touch me, feel free to pass me out, or just give me the variables and I'll take a look at nothing.
Grammatically:
Constexpr is a stricter constraint than const, the expression itself is known during compilation, and the compiler will evaluate at compile time as much as possible. Before the advent of constexpr, the const that could be initialized at compile time was implicit constexpr. It was not until C++ 11 that constexpr was subdivided from const to become a keyword, while const has existed since C++ first changed its name in 1983. If you are a beginner to censor, you should use constexpr as much as possible to help the compiler optimize the code.
This is the answer to the question about the usage analysis of Clear11 constexpr. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.