In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the function of C++ inline function". In daily operation, I believe many people have doubts about the function of C++ inline function. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt about "what is the function of C++ inline function?" Next, please follow the editor to study!
C++ inline functions can not only remove the efficiency burden caused by function calls, but also retain the advantages of general functions. However, inline functions are not drugs, and in some cases they can even degrade the performance of programs. Therefore, it should be used with caution.
1. let's first look at the benefits of inline functions: from a user's point of view, inline functions look the same as ordinary functions, they can have parameters and return values, or they can have their own scope. however, it does not introduce the burden of general function calls. In addition, it can be safer and easier to debug than macros.
Of course, you should be aware that inline specifier is only a recommendation to the compiler, and the compiler has the right to ignore this recommendation. So how does the compiler decide whether a function is inlined or not? In general, key factors include the size of the function body, whether any local objects are declared, the complexity of the function, and so on.
2. What happens if a function is declared as inline but is not inlined? In theory, when the compiler refuses to inline a function, that function will be treated like a normal function, but there will be other problems. For example, the following code:
/ / filename Time.h
# include
< ctime># include
< iostream>Using namespace std
Class Time
{
Public:
Inline void Show () {for (int I = 0; I < 10; iTunes +)
Cout < < time (0) < < endl;}
}
Because the member function Time::Show () includes a local variable and a for loop, the compiler generally rejects inline and treats it as a normal member function. But the header file that contains the class declaration is put into a separate compilation unit by a separate # include:
/ / filename f1.cpp # include "Time.hj" void F1 () {Time T1; t1.Show ();} / / filename f2.cpp # include "Time.h" void f2 () {Time T2; t2.Show ();}
As a result, the compiler generated two copies of the same member functions for the program:
Void F1 (); void f2 (); int main () {F1 (); f2 (); return 0;}
When the program is linked, linker will face two identical copies of Time::Show (), so a connection error redefined by the function occurs. But the older C++ implementation dealt with this situation by treating a un-inlined function as if it were static. So each copy of the function is visible only in its own compilation unit, so the link error is resolved, but multiple copies of the function are left in the program. In this case, instead of improving the performance of the program, it increases the compilation and linking time and the size of the final executable.
But fortunately, the new C++ standard about un-inlined functions has changed. A standard C++ implementation should generate only one copy of the function. However, it may take a long time for all compilers to support this.
There are also two more troubling questions about C++ inline functions. The question is how to maintain it. A function may appear inline at first, but as the system expands, the function body may require additional functionality, making inline functions less likely, so you need to remove inline specifier and put the function body in a separate source file. Another problem is when inline functions are applied to the code base. When inline functions change, users must recompile their code to reflect the change. For a non-inline function, however, the user simply needs to relink.
What I want to say here is that inline functions are not a panacea to enhance performance. Only when the function is very short can it get the effect we want, but if the function is not very short and is called in many places, it will increase the size of the executable. The most annoying thing is when the compiler refuses to inline. In the old implementation, the result is not satisfactory. Although there is a great improvement in the new implementation, it is still not perfect. Some compilers are smart enough to point out which functions can be inlined and which are not, but most compilers are not so smart, so it takes our experience to judge. If the C++ inline function does not enhance energy, avoid using it!
At this point, the study of "what is the role of C++ inline function" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.