26

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

4
  • 3
    note: inline is only hint to the compiler. The compiler is not required to inline it. Sep 18, 2009 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, 2009 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, 2009 at 23:19
  • @rstevens: You already have that; it's spelled #define.
    – MSalters
    Sep 21, 2009 at 11:42

11 Answers 11

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

1
  • 1
    This is the really important aspect of inline.
    – Jason S
    Sep 19, 2009 at 13:32
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.

7
  • 5
    Since compiler does everything for us, why on earth we need an inline hint? Nov 4, 2010 at 2: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. Nov 4, 2010 at 4:14
  • 1
    One more thing: it also allows you to define the function in more than one compilation unit Nov 5, 2010 at 5: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. Nov 5, 2010 at 5:29
  • 1
    @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. Nov 5, 2010 at 6:22
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.

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.

4

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

1
  • "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, 2009 at 12:45
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;

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.

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.

1

Compiler optimize as he wants unless you spec the opposite.

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

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)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.