1

Possible Duplicate:
When to use inline function and when not to use it?

Under what conditions can (not should) a function not be inlined (C++ Only) ?

Two conditions that i know of are :

1 . If the function has a recursive call

2 . If there are static variables in the function

8
  • Note, ability to be inlined doesn't necessary mean it will. The final decision belongs to the compiler, no matter what compilation flags you're using Commented Nov 18, 2012 at 7:36
  • I am not asking when i should not, but when i cannot .. Commented Nov 18, 2012 at 7:40
  • What are you references for 2)? Commented Nov 18, 2012 at 7:40
  • Neither of the conditions you name necessarily prevent inlining a function. Commented Nov 18, 2012 at 7:40
  • books.google.co.in/… Commented Nov 18, 2012 at 7:43

2 Answers 2

4

inline is a keyword of C++, but inlining is a generic process performed by a compiler backend, usually after instruction sequences are already generated.

A C compiler will also inline functions, and a C++ compiler will inline functions that aren't inline. A C++ compiler can also fail to inline an inline function for any arbitrary reason. The keyword actually exists to specify that a function may have multiple, identical definitions in different translation units (source files).

Static variables have no special bearing on whether something can be inlined. Perhaps some compilers have difficulty linking the resulting structure of global variable references, but that's more of a bug than a rule of thumb.

Recursive functions can be inlined, too. The recursive call should be translated to a branch. The branch could then be targeted by loop unrolling.

A function that compiles to more than a kilobyte of code will usually not be inlined. But a compiler may provide #pragma directives or platform-specific attributes to force inlining in such a case.

The biggest factor that would stop a function from being inlined is if its source isn't available to the compiler at the time of code generation. Link-time optimization opens the possibility of inlining functions that are extern and not inline but a function supplied by a DLL is certainly off limits. But then, you could still run it through a JIT style execution engine and that could inline (splice together) any random fragments it likes.

Sign up to request clarification or add additional context in comments.

Comments

1

The only situation in which a function cannot be inlined is if there is no definition for the function in the compilation unit. Even that will not prevent link-time inlining by a link-time optimizer.

Note that the inline keyword is really just a hint -- a compiler may choose not to inline functions with it and choose to inline functions without it.

5 Comments

What problem is the author referring to then ?? books.google.co.in/… I cant understand the first two conditions or why they are ..
@AshRj That book is talking rubbish. The author doesn't understand inline. It's a very common misunderstanding. Potatoswatters answer looks good to me. It's important to understand that the inline keyword in C++ and whether the compiler does inline a function are not the same thing. A compiler might inline a function you didn't tell it to, and it might not inline a function you did tell it to. The compiler can do what it likes as long as it runs your program correctly.
@john you are the third person on SO to tell me the book is rubbish ! Cant do anything about it .. it is considered a good introduction book to OOP with C++ by my professor
@AshRj I was only talking about the section on inline. I haven't read the rest of the book.
@john I have,and i agree with you :) Most of the sections are like this page only .. I dont understand why its used in our university :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.