Does the compiler decide when to inline my functio

2019-01-15 08:16发布

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?

9条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-15 08:34

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

查看更多
疯言疯语
3楼-- · 2019-01-15 08:38

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

查看更多
Melony?
4楼-- · 2019-01-15 08:39

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楼-- · 2019-01-15 08:40

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.

查看更多
在下西门庆
6楼-- · 2019-01-15 08:41

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.)

查看更多
Lonely孤独者°
7楼-- · 2019-01-15 08:43

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.

查看更多
登录 后发表回答