问题
I understand you can use the inline keyword or just put a method in a class declaration ala short ctor or a getter method, but does the compiler make the final decision on when to inline my methods?
For instance:
inline void Foo::vLongBar()
{
//several function calls and lines of code
}
Will the compiler ignore my inline declaration if it thinks it will make my code inefficient?
As a side issue, if I have a getter method declared outside my class like this:
void Foo::bar() { std::cout << "baz"; }
Will the compiler inline this under the covers?
回答1:
Whether or not a fiunction is inlined is, at the end of the day, entirely up to the compiler. Typically, the more complex a function is in terms of flow, the less likely the compiler is to inline it. and some functions, such as recursive ones, simply cannot be inlined.
The major reason for not inlining a function is that it would greatly increase the overall size of the code, preventing iot from being held in the processor's cache. This would in fact be a pessimisation, rather than an optimisation.
As to letting the programmer decide to shoot himself in the foot, or elsewhere, you can inline the function yourself - write the code that would have gone in the function at what would have been the function's call site.
回答2:
Yes, the final decision of whether or not to inline your code lies in the C++ compiler. The inline keyword is a suggestion, not a requirement.
Here are some details as to how this decision is processed in the Microsoft C++ Compiler
- http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx
回答3:
As many have already posted, the final decision is always up to the compiler, even if you can give firm hints such as forceinline.
Part of the rationale is that inlining is not an automatic "go faster" switch. Too much inlining can make your code much larger, and may interfere with other optimizations. See The C++ FAQ Lite about inline functions and performance.
回答4:
As others have noted, the inline
keyword is merely a suggestion to the compiler to inline the code. Since the compiler will routinely inline code that hasn't been marked with inline
, and not inline code that has, the keyword seems just as redundant as register
or (pre-C++0x) auto
.
However, there is one other thing that the inline
keyword effects: It changes the linkage of the function from external (the default for functions) to inline. Inline linkage allows each compilation unit to contain it's own copy of the object code, and has the linker remove redundant copies from the final executable. If that reminds you of templates, yes, templates also use inline linkage.
回答5:
Yes, the compiler has the final decision. In VS you can even inline recursive functions into specified depth ;)
#pragma inline_depth( [0... 255] )
回答6:
Just to add my 5 cents ...
I found this Guru of Week article about inlining very useful.
As far as I remember I read somewhere that even a linker might do inlining, when it links the object files and finds that the code being linked can be inlined.
Regards,
Ovanes
回答7:
As a side issue, if I have a getter method declared outside my class like this:
void Foo::bar() { std::cout << "baz"; }
Will the compiler inline this under the covers?
It depends. It can for all callers in the same translation unit (the .cpp
file and all of its #included definitions). But it still has to compile a non-inlined version because there may be callers of that function outside the translation unit. You can potentially see this at work (if your compiler actually can do it) at high optimization levels. (In particular: compare what happens when you #include all your .cpp files in one .cpp vs. the typical layout. With all definitions in one translation unit the opportunities for such inlining increase dramatically.)
回答8:
As per my knowledge, compiler will automatically make a function you declared inline (or wrote inside a class declaration) non -inline if it finds a loop like for, while etc. This is one example where compiler has the last say in inline functions.
回答9:
If you really, positively, absolutely, without fail NEED to inline the code, there is always the macro. C has supported these for years and because they are just text replacement prior to compilation, they really, truly, inline whatever you write.
That's why the 'inline' keyword (and even, in some cases, the forced variants) can afford to not have a standard way of forcing it - you can always just write a macro.
That said, the inline keyword is often better, because the compiler quite often knows if making a function inline makes sense or not, and because the inline can interact with the rest of the compiler optimizations.
来源:https://stackoverflow.com/questions/1204975/does-the-compiler-decide-when-to-inline-my-functions-in-c