views:

660

answers:

10

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?

+4  A: 

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

Nick D
"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
@MSalters, you're right.
Nick D
+15  A: 

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
+2  A: 

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
+2  A: 

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
+2  A: 

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;

adatapost
A: 

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
A: 

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
+1  A: 

Compiler optimize as he wants unless you spec the opposite.

+6  A: 

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
+1 for pointing out the "double meaning" of `inline`
peterchen
This is the really important aspect of inline.
Jason S
A: 

Your compilers 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 effect as static, and each compilation unit that calls the code will have its own copy.

Clifford

Clifford