StackedCrooked
StackedCrooked

Reputation: 35485

Usefulness of the "inline" feature

There's two things about inlining:

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

Answers (8)

mip
mip

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

Loki Astari
Loki Astari

Reputation: 264361

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

Upvotes: 0

Mattias Nilsson
Mattias Nilsson

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

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247909

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.

Upvotes: 12

KeithB
KeithB

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

matt-dot-net
matt-dot-net

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

Greg D
Greg D

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

anon
anon

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

Related Questions