In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about the C++inline function. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
1. The role of inline (inline function)
We use inline keyword and function definition together can create an inline function, its role is to reduce the cost of function calls, if our program has a function will be frequently called, so that the program runs very slowly, then we use inline function, can solve this problem, improve the efficiency of the program.
Definition of inline function:
inline void add(int a, int b){ return a+b;}
This is an inline function. If we use add frequently in our program, declaring it inline will reduce the call overhead and improve efficiency.
Note:
1. inline keyword is a "keyword for implementation", not "keyword for declaration", in other words inline only before the definition of the function, it is possible to make the function inline, placed before the declaration of the function invalid.
2. Another point is: in c++ class member functions defined by default is inline, but the premise is defined in the class, because we normally define the class in the header file declaration of member variables and member functions, in the original file on the class member functions defined, at this time the definition of member functions occurs outside the class, so naturally no longer an inline function, if you want to make it an inline function, we need to manually add inline.
3. As for the first point, why do I say that the function may become an inline function, because inline is a recommended keyword, he does not guarantee that the function will become an inline function, but the compiler at the time of compilation to obtain inline suggested function to its size, content, method and other elements to judge, if the compiler requirements will become an inline function, otherwise will not become an inline function, so whether or not to become an inline function is entirely up to the compiler.
II. Implementation of inline (note that inline functions do not expand in debug mode)
Since inline functions reduce the cost of calling functions, how do they do it? Very simply, we expand the code at all places where inline functions are called, which increases the amount of code but reduces the overhead of calls, a typical space-for-time approach.
We set the compiler to Release mode to use inline functions, and you can see how they are implemented under assembly instructions.
Source code:
#includeusing namespace std;inline int add(int a, int b) { return 0;}int main() { int b=add(1, 2); cout
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.