[ACCEPTED]-When do compilers inline C++ code?-inline

Accepted answer
Score: 43

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

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

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

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

Score: 30

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

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

Score: 5

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

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

Score: 4

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

Score: 4

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

Score: 3

Text from IBM information Center,

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

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

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

Score: 2

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

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

Score: 1

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

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

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

Score: 1

Compiler optimize as he wants unless you 1 spec the opposite.

Score: 0

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

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

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

If anybody 1 knows other factors involved, please post.

Score: 0

Some of the situations where inline expansion 9 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 8 a program run faster because the overhead 7 of a function call and return statement 6 is eliminated. However, it makes the program 5 to take up more memory because the statements 4 that define the inline functions are reproduced 3 at each point where the function is called. So, a 2 trade-off becomes necessary.

(As given in 1 one of my OOP books)

More Related questions