Is there any difference to the following code:
class Foo
{
inline int SomeFunc() { return 42; }
int AnotherFunc() { return 42; }
};
Will both functions gets inlined? Does inline actually make any difference? Are there any rules on when you should or shouldn't inline code? I often use the AnotherFunc
syntax (accessors for example) but I rarely specify inline
directly.
Both forms should be inlined in the exact same way. Inline is implicit for function bodies defined in a class definition.
The inline
keyword is essentially a hint to the compiler. Using inline
doesn't guarantee that your function will be inlined, nor does omitting it guarantee that it won't. You are just letting the compiler know that it might be a good idea to try harder to inline that particular function.
Sutter's Guru of the Week #33 answers some of your questions and more.
class Foo
{
inline int SomeFunc() { return 42; }
int AnotherFunc() { return 42; }
};
It is correct that both ways are guaranteed to compile the same. However, it is preferable to do neither of these ways. According to the C++ FAQ you should declare it normally inside the class definition, and then define it outside the class definition, inside the header, with the explicit inline keyword. As the FAQ describes, this is because you want to separate the declaration and definition for the readability of others (declaration is equivalent to "what" and definition "how").
Does inline actually make any difference?
Yes, if the compiler grants the inline request, it is vastly different. Think of inlined code as a macro. Everywhere it is called, the function call is replaced with the actual code in the function definition. This can result in code bloat if you inline large functions, but the compiler typically protects you from this by not granting an inline request if the function is too big.
Are there any rules on when you should or shouldn't inline code?
I don't know of any hard+fast rules, but a guideline is to only inline code if it is called often and it is relatively small. Setters and getters are commonly inlined. If it is in an especially performance intensive area of the code, inlining should be considered. Always remember you are trading execution speed for executable size with inlining.
VC++ supports __forceinline and __declspec(noinline) directives if you think you know better than the compiler. Hint: you probably don't!
Inline is a compiler hint and does not force the compiler to inline the code (at least in C++). So the short answer is it's compiler and probably context dependent what will happen in your example. Most good compilers would probably inline both especially due to the obvious optimization of a constant return from both functions.
In general inline is not something you should worry about. It brings the performance benefit of not having to execute machine instructions to generate a stack frame and return control flow. But in all but the most specialized cases I would argue that is trivial.
Inline is important in two cases. One if you are in a real-time environment and not responding fast enough. Two is if code profiling showed a significant bottleneck in a really tight loop (i.e. a subroutine called over and over) then inlining could help.
Specific applications and architectures may also lead you to inlining as an optimization.
I have found some C++ compilers (I.e. SunStudio) complain if the inline is omitted as in
int AnotherFunc() { return 42; }
So I would recommend always using the inline keyword in this case. And don't forget to remove the inline keyword if you later implement the method as an actual function call, this will really mess up linking (in SunStudio 11 and 12 and Borland C++ Builder). I would suggest making minimal use of inline code because when stepping through code with with a debugger, it will 'step into' the inline code even when using 'step over' command, this can be rather annoying.
Note that outside of a class, inline
does something more useful in the code: by forcing (well, sort of) the C++ compiler to generate the code inline at each call to the function, it prevents multiple definitions of the same symbol (the function signature) in different translation units.
So if you inline a non-member function in a header file, and include that in multiple cpp files you don't have the linker yelling at you. If the function is too big for you to suggest inline-ing, do it the C way: declare in header, define in cpp.
This has little to do with whether the code is really inlined: it allows the style of implementation in header, as is common for short member functions.
(I imagine the compiler will be smart if it needs a non-inline rendering of the function, as it is for template functions, but...)
Also to add to what Greg said, when preforming optimization (i.e. inline
-ing) the compiler consults not only the key words in the code but also other command line arguments the specify how the compiler should optimize the code.
来源:https://stackoverflow.com/questions/86561/inlining-c-code