26

In C++, do methods only get inlined if they are explicitly declared inline (or defined in a header file), or are compilers allowed to inline methods as they see fit?

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
  • 3
    note: inline is only hint to the compiler. The compiler is not required to inline it. – Martin York Sep 18 '09 at 16:29
  • @Martin York: Unfortunately you're right. And in 99.9% of the cases it is the best to let the compiler decide in the last instance. But in some cases (performance critical programs like Image processing, etc.) it would be nice to have a ultimate inline forcing switch. But I can understand the compiler programmers. Because as soon as this switch would exist many would use it in many inappropriate cases no matter how insistently the documentation warns to only use it in rare cases... – mmmmmmmm Sep 18 '09 at 20:12
  • 1
    More accurately, `inline` has a different meaning in C/C++ code than the one used to describe the optimization. In C/C++ code, `inline` simply means that multiple definitions of a function may exist, and that the linker should merge them back together. It does *not* mean that calls to that function should be inlined. They're two entirely separate concepts. – jalf Sep 20 '09 at 23:19
  • @rstevens: You already have that; it's spelled `#define`. – MSalters Sep 21 '09 at 11:42

11 Answers11

45

The inline keyword really just tells the linker (or tells the compiler to tell the linker) that multiple identical definitions of the same function are not an error. You'll need it if you want to define a function in a header, or you will get "multiple definition" errors from the linker, if the header is included in more than one compilation unit.

The rationale for choosing inline as the keyword seems to be that the only reason why one would want to define a (non-template) function in a header is so it could be inlined by the compiler. The compiler cannot inline a function call, unless it has the full definition. If the function is not defined in the header, the compiler only has the declaration and cannot inline the function even if it wanted to.

Nowadays, I've heard, it's not only the compiler that optimizes the code, but the linker can do that as well. A linker could (if they don't do it already) inline function calls even if the function wasn't defined in the same compilation unit.

And it's probably not a good idea to define functions larger than perhaps a single line in the header if at all (bad for compile time, and should the large function be inlined, it might lead to bloat and worse performance).

UncleBens
  • 40,819
  • 6
  • 57
  • 90
31

Yes, the compiler can inline code even if it's not explicitly declared as inline.

Basically, as long as the semantics are not changed, the compiler can virtually do anything it wants to the generated code. The standard does not force anything special on the generated code.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 5
    Since compiler does everything for us, why on earth we need an inline hint? – Baiyan Huang Nov 04 '10 at 02:42
  • 2
    @lz_prgmr: Well, besides the subtle point is mentioned by UncleBens below, I think we don't. It's important to note that compilers *were* not that clever from the beginning and we are kind of stuck with old features of the language. – Mehrdad Afshari Nov 04 '10 at 04:14
  • 1
    One more thing: it also allows you to define the function in more than one compilation unit – Baiyan Huang Nov 05 '10 at 05:18
  • Even though, I start to question the correctness of your answer: If a function is not declared as inline but implemented in header file, the compiler will complain about link errors, rather than inline them as default if appears in multiple translation unit. – Baiyan Huang Nov 05 '10 at 05:29
  • @lz_prgmr: It affects the "complaining" behavior of the compiler, as pointed out, but that doesn't mean the compiler is forced to inline it or anything in that regard. The compiler is free to inline anything it likes and not inline even if you decorate a function with `inline`. – Mehrdad Afshari Nov 05 '10 at 06:22
  • So the "inline" keyword is necessary, with which we even can't compile successfully, not to mention to inline it. – Baiyan Huang Nov 05 '10 at 09:36
  • This makes debugging using GDB / WinDBG difficult. – Jayanth Feb 01 '23 at 15:15
5

Compilers might inline any function or might not inline it. They are allowed to use the inline decoration as a hint for this decision, but they're also allowed to ignore it.

Also note that class member functions have an implicit inline decoration if they are defined right in the class definition.

sbi
  • 219,715
  • 46
  • 258
  • 445
4

If I'm not mistaken, when optimizations are turned on, the compiler will inline any suitable routine or method.

Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
  • "Turning optimizations on" is not a standard term. You're describing actual implementations, who do have that switch. In that case you don't need the hypothetical _may_: The compiler _will_ inline suitable functions. – MSalters Sep 18 '09 at 12:45
4

Compilers may ignore your inline declaration. It is basically used by the compiler as a hint in order decide whether or not to do so. Compilers are not obligated to inline something that is marked inline, or to not inline something that isn't. Basically you're at the mercy of your compiler and the optimization level you choose.

RC.
  • 27,409
  • 9
  • 73
  • 93
3

Text from IBM information Center,

Using the inline specifier is only a suggestion to the compiler that an inline expansion can be performed; the compiler is free to ignore the suggestion.

C Language Any function, with the exception of main, can be declared or defined as inline with the inline function specifier. Static local variables are not allowed to be defined within the body of an inline function.

C++ functions implemented inside of a class declaration are automatically defined inline. Regular C++ functions and member functions declared outside of a class declaration, with the exception of main, can be declared or defined as inline with the inline function specifier. Static locals and string literals defined within the body of an inline function are treated as the same object across translation units;

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2

Your compiler's documentation should tell you since it is implementation dependent. For example, GCC according to its manual never inlines any code unless optimisation is applied.

If the compiler does not inline the code, the inline keyword will have the same effect as static, and each compilation unit that calls the code will have its own copy. A smart linker may reduce these to a single copy.

Clifford
  • 88,407
  • 13
  • 85
  • 165
1

The compiler can inline whatever it wants in case inlining doesn't violate the code semantics and it can reach the function code. It can also inline selectively - do inline when it feels it's a good idea and not inline when it doesn't feel it's a good idea or when it would violate the code semantics.

Some compilers can do inlining even if the function is in another translation unit - that's called link-time code generation.

Typical cases of when inlining would violate code semantics are virtual calls and passing a function address into another function or storing it.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
1

Compiler optimize as he wants unless you spec the opposite.

user175491
  • 39
  • 2
0

The inline keyword is just a request to the compiler. The compiler reserves the right to make or not make a function inline. One of the major factor that drives the compiler's decision is the simplicity of code(not many loops)

Member functions are declared inline by default.(The compiler decides here also)

These are not hard and fast rules. It varies according to the compiler implementations.

If anybody knows other factors involved, please post.

santhosh
  • 111
  • 1
  • 3
  • 6
0

Some of the situations where inline expansion may NOT work are:

  1. For functions returning values, if a loop, a switch, or a goto exists
  2. For function not returning values, if a return statement exits;
  3. If functions contain static variables
  4. If inline functions are recursive.

Inline expansion makes a program run faster because the overhead of a function call and return statement is eliminated. However, it makes the program to take up more memory because the statements that define the inline functions are reproduced at each point where the function is called. So, a trade-off becomes necessary.

(As given in one of my OOP books)

diwakar
  • 21
  • 4