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

Community
  • 1
  • 1
asheeshr
  • 4,088
  • 6
  • 31
  • 50

2 Answers2

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • What problem is the author referring to then ?? http://books.google.co.in/books?id=TN9wQjjDwp0C&lpg=PP1&pg=PA83#v=onepage&q&f=false I cant understand the first two conditions or why they are .. – asheeshr Nov 18 '12 at 07:49
  • @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 Nov 18 '12 at 08:48
  • @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 – asheeshr Nov 18 '12 at 12:35
  • @AshRj I was only talking about the section on inline. I haven't read the rest of the book. – john Nov 18 '12 at 14:26
  • @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 :/ – asheeshr Nov 18 '12 at 14:28