89487

What is the criterion to be inline [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method" dir="ltr" rel="nofollow">When should I write the keyword 'inline' for a function/method?</a> <span class="question-originals-answer-count"> 12 answers </span> </li> </ul>

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

<strong>Extra question</strong>. Can I make an inline call?

void func{} // normal (not-inline) function int main() { func(); // normal call // inline func(); // inline call return 0; }

Answer1:

<blockquote>

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

</blockquote> <ul><li>

(from <a href="http://en.wikipedia.org/wiki/Inline_function" rel="nofollow">wiki</a>) 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.

</li> </ul><blockquote>

What kind of functions should I make inline?

</blockquote> <ul><li>

Small functions. It's a good idea to define small (as in one liner) functions in the header file as it gives the compiler more information to work with while optimizing your code. It also increases compilation time.

Check out <a href="https://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method" rel="nofollow">When should I write the keyword 'inline' for a function/method?</a> for more info.

</li> </ul><blockquote>

Can I make an inline call?

</blockquote> <ul><li>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.</li> </ul>

Answer2:

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.

Answer3:

What inline does is <em>recommend</em> 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 <em>have</em> to do this, but if it does, this is what happens:

If you have this function:<br />

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

Recommend

  • Numpy array pass-by-value
  • Alternative of Action done button in HTC desire?
  • HTML5 tags and WAI ARIA [duplicate]
  • Client-side prediction & server reconciliation
  • nested (double) loop with thymeleaf
  • Delphi: StringGrid, position and Context Menu
  • Linked tables and Slicer in excel
  • iPhone: 5 seconds video capture
  • How can I see all the files that were modified/added/removed in the last push received?
  • Printing out Japanese (Chinese) characters
  • How to discover a file is changed in GIT during a push
  • C: Custom strlen() library function
  • manupulating a string to create directories in unix
  • CSS: glowing text with glow very wide and high
  • How to format data from string variable
  • javascript variables, What does var x = a = {} do?
  • Difference between two forms of Perl filename wildcard command
  • Skip Characters in Oracle TO_DATE function
  • help('modules') crashing? Not sure how to fix
  • Why does Sencha 2 only work in Webkit browsers?
  • Can XOR be expressed using SKI combinators?
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • Object and struct member access and address offset calculation
  • uniform generation of points on 3D box
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • Why winpcap requires both .lib and .dll to run?
  • PHP: When would you need the self:: keyword?
  • Linker errors when using intrinsic function via function pointer
  • KeystoneJS: Relationships in Admin UI not updating
  • Acquiring multiple attributes from .xml file in c#
  • How to set the response of a form post action to a iframe source?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Does armcc optimizes non-volatile variables with -O0?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF