0

How does compiler calculate which function should be inline since inline request is not obligatory? What kind of functions should I make inline?

Extra question. Can I make an inline call?

void func{} // normal (not-inline) function

int main()
{
  func();           // normal call
  // inline func(); // inline call
  return 0;
}
Ivars
  • 2,375
  • 7
  • 22
  • 31
  • 1
    Just check documentation for your compiler (both for syntax and for applicability). It'll tell you when it'll inline function calls. BTW no, syntax in the commented line is not valid.In general it's a good thing to inline a small/fast function: if function takes 1 second to execute then to save 10 ns for function call it's pretty useless. – Adriano Repetti Jan 16 '14 at 20:15
  • @Adriano thanks i know that's why i have commented it) – Ivars Jan 16 '14 at 20:17
  • 4
    The `inline` keyword is just a **hint** for the compiler, no more, no less. All of the rest is compiler implementation specific. – πάντα ῥεῖ Jan 16 '14 at 20:19
  • The compiler usually inlines a function if it thinks it will produce better code by doing it. Normally you can rely on "small" functions being inlined if they are visible in the current compilation unit. – Tim Seguine Jan 16 '14 at 20:23
  • Do you mean "`inline`", or do you mean "inlined"? – Lightness Races in Orbit Jan 16 '14 at 20:33
  • @LightnessRacesinOrbit what's the difference? – Ivars Jan 16 '14 at 20:34
  • @user2543574: Inlining is when the compiler takes your function body and mentally replaces the call with the actual code, to save a bit of overhead. `inline` is a keyword that _hints_ to the compiler that it might want to do this; however, these days, compilers are clever enough to figure it out on their own and will do what they want anyway, so we really only use `inline` to get around the one-definition-rule and define our functions in headers. – Lightness Races in Orbit Jan 16 '14 at 20:35
  • @LightnessRacesinOrbit guess i meant both) – Ivars Jan 16 '14 at 20:40
  • I suggest you read the accepted answer in the linked thread carefully. Answers given here are incomplete or give misinformation. – jrok Jan 16 '14 at 20:47

3 Answers3

5

When you are mark a function as inline you are advising the compiler that this function should be inlined. On (good) compilers this neither guarantees it will be, nor does it's absence guarantee it won't be.

All inlining a function does is replace the call to the function with the body of the code. So while you can't suggest where the compiler should inline the function, you obviously could actually paste the code in (or use a macro to ensure the code stays the same). But you'd need a really good/odd reason to do this.

In general, mark small functions as inline if you wish, but otherwise leave it to the compiler.

T. Kiley
  • 2,752
  • 21
  • 30
2

How does compiler calculate which function should be inline since inline request is not obligatory?

  • (from wiki) In various versions of the C and C++ programming languages, an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.

    In C++, you can use __forceinline keyword allow the programmer to force the compiler to inline the function, but indiscriminate use of __forceinline can result in larger code (bloated executable file), minimal or no performance gain, and in some cases even a loss in performance.

What kind of functions should I make inline?

Can I make an inline call?

  • You cannot make an inline call as inline can only used in function declaration, not calling. You just need to call a pre-defined inline function as normal functions.
Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
0

What inline does is recommend to the compiler to replace any calls to this function with the actual code of the function, as T. Kiley said a while ago. The compiler does not have to do this, but if it does, this is what happens:

If you have this function:

inline int addtwo(int i)
{
return i+2;
}

the compiler will replace all statements like

cout<<addtwo(x)<<endl;

with

cout<<(x+2)<<endl;

If your function was longer than a few lines, the finished executable size would increase, since every call would be replaced with the entire code of the function. (what other drawbacks are there?)

inline is for functions that are trivially short (1 line).

  • 3
    This is not true. `inline` does *not* tell the compiler to do this for all calls, as the question says and several other answers have also said. – Steve Jessop Jan 16 '14 at 20:43
  • I have edited the post to make clear that the compiler doesn't have to do this - is that what was wrong? –  Jan 17 '14 at 19:37