Reputation: 35485
There's two things about inlining:
inline
keyword will be ignored if the compiler determines that the function cannot be inlined.From this I conclude that I never need to bother about inlining. I just have to turn on the compiler optimization for the release build.
Or are there any situations where manually inlining would be preferred?
Upvotes: 4
Views: 491
Reputation: 8713
There's a side effect of inline
keyword when you are building shared library. Inlined functions are not exported into symbol table nor into library's binary. As a result inline
keyword is crucial in aspect of shared libraries, since compiler won't be able to inline exported function. On the other hand library's inline function will be always inlined because it doesn't exist in the binary form of the library.
Upvotes: 1
Reputation: 264361
Read Herb Sutters comments on inline:
http://www.gotw.ca/gotw/033.htm
Upvotes: 0
Reputation: 3757
It depends on your environment and what you want to do, so it is really hard to say when inlining is preferrable.
This link has some interesting reading about inlining. And some sound advice (which pretty much boils down to: avoid doing it)
Upvotes: 0
Reputation: 247909
The inline
keyword has two functions:
inline
'd symbol may be defined in multiple translation units (typically because it is defined in a header, that is included from multiple files). Normally, this would result in a linker error, but it is allowed when you use the inline
keyword.Upvotes: 12
Reputation: 17007
Inline is also useful if you want the ability to inline functions from a library. Only by putting the code for the function in the header file (which requires inline), is the compiler able to inline the function. Of course it is still up the the compiler whether to inline the function or not.
Upvotes: 1
Reputation: 4244
You may not want to inline everywhere it is possible. This could increase the size of your binaries too much. You may have a select few functions that aren't used very much that inlining would allow to run faster without increasing the size of your bits significantly
Upvotes: 0
Reputation: 44066
Manual use of inline
might be useful on older compilers or less sophisticated compilers (such as compilers for embedded development). If you're using visual studio, I don't think you typically need to use the inline
keyword at all.
Upvotes: 1
Reputation:
Yes, if you want to put a function in a header file, and include that file in several translation units. This is in fact the main purpose of inline
in C++.
Upvotes: 7