tags:

views:

183

answers:

8

There's two things about inlining:

  • The inline keyword will be ignored if the compiler determines that the function cannot be inlined.
  • There is a compiler optimization (on Visual Studio, I don't know about GCC) that tells the compiler to inline all functions where possible.

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?

+6  A: 

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

anon
Thanks, I didn't know that inline could be used the purpose of avoiding multiple definitions. However, are there also any reasons why one might prefer manual inlining for optimization reasons?
StackedCrooked
@StackedCrooked: you can't "prefer" one or the other. The compiler will inline if it likes, whether or not you mark a function as `inline`.
jalf
+1  A: 

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.

Greg D
A: 

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

matt-dot-net
+1  A: 

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.

KeithB
+9  A: 

The inline keyword has two functions:

  • it serves as a hint to the compiler to perform the inlining optimization (this is basically useless on modern compilers, which inline aggressively with or without the keyword)
  • it tells the compiler/linker to ignore the One Definition Rule: that the 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.
jalf
The first bullet is a direct answer to my question. Thanks.
StackedCrooked
Not in all cases it's true. In case of shared libraries `inline` keyword is crucial.
doc
@doc: can you elaborate?
jalf
A: 

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)

Mattias Nilsson
A: 

Read Herb Sutters comments on inline:
http://www.gotw.ca/gotw/033.htm

Martin York
A: 

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.

doc