<p>While it would be very convenient to use inline functions at some situations,</p>

<p>Are there any drawbacks with inline functions?</p>

<p><strong>Conclusion</strong>:</p>

<p>Apparently, There is nothing wrong with using inline functions.</p>

<p>But it is worth noting the following points!</p>

<ul>
<li><p>Overuse of inlining can actually make programs slower. Depending on a function's size, inlining it can cause the code size to increase or decrease. Inlining a very small accessor function will usually decrease code size while inlining a very large function can dramatically increase code size. On modern processors smaller code usually runs faster due to better use of the instruction cache. <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Inline_Functions" rel="noreferrer">- Google Guidelines</a></p></li>
<li><p>The speed benefits of inline functions tend to diminish as the function grows in size. At some point the overhead of the function call becomes small compared to the execution of the function body, and the benefit is lost <a href="http://www-rocq.inria.fr/who/Marc.Thiriet/Langages/tic0324.html" rel="noreferrer">- Source</a></p></li>
<li><p>There are few situations where an inline function may not work:</p>

<ul>
<li>For a function returning values; if a return statement exists.</li>
<li>For a function not returning any values; if a loop, switch or goto statement exists. </li>
<li>If a function is recursive. <a href="http://www-rocq.inria.fr/who/Marc.Thiriet/Langages/tic0324.html" rel="noreferrer">-Source</a></li>
</ul></li>
<li><p>The <code>__inline</code> keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not <code>__inline</code> is honored depends on the setting of the inline optimizer option. By default, the inline option is in effect whenever the optimizer is run. If you specify optimize , you must also specify the noinline option if you want the <code>__inline</code> keyword to be ignored. <a href="http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/clug/zcoptinl.htm" rel="noreferrer">-Source</a></p></li>
</ul>

[{'answer_id': 60833, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 2, 'selected': False, 'text': '<p>I doubt it. Even the compiler automatically inlines some functions for optimization. </p>\n'}, {'answer_id': 60835, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 4, 'selected': False, 'text': '<p>It could increase the size of the\n executable, and I don\'t think\n compilers will always actually make\n them inline even though you used the\n inline keyword. (Or is it the other\n way around, like what <a href="https://stackoverflow.com/questions/60830/what-is-wrong-with-using-inline-functions#60833">Vaibhav</a>\n said?...)</p>\n\n<p>I think it\'s usually OK if the\n function has only 1 or 2 statements.</p>\n\n<p><strong>Edit:</strong> Here\'s what the linux <a href="http://lxr.linux.no/linux/Documentation/CodingStyle" rel="nofollow noreferrer">CodingStyle</a> document says about it:</p>\n\n<blockquote>\n  <p>Chapter 15: The inline disease</p>\n  \n  <p>There appears to be a common\n  misperception that gcc has a magic\n  "make me faster" speedup option called\n  "inline". While the use of inlines can\n  be appropriate (for example as a means\n  of replacing macros, see Chapter 12),\n  it very often is not. Abundant use of\n  the inline keyword leads to a much\n  bigger kernel, which in turn slows the\n  system as a whole down, due to a\n  bigger icache footprint for the CPU\n  and simply because there is less\n  memory available for the pagecache.\n  Just think about it; a pagecache miss\n  causes a disk seek, which easily takes\n  5 miliseconds. There are a LOT of cpu\n  cycles that can go into these 5\n  miliseconds.</p>\n  \n  <p>A reasonable rule of thumb is to not\n  put inline at functions that have more\n  than 3 lines of code in them. An\n  exception to this rule are the cases\n  where a parameter is known to be a\n  compiletime constant, and as a result\n  of this constantness you <em>know</em> the\n  compiler will be able to optimize most\n  of your function away at compile time.\n  For a good example of this later case,\n  see the kmalloc() inline function.</p>\n  \n  <p>Often people argue that adding inline\n  to functions that are static and used\n  only once is always a win since there\n  is no space tradeoff. While this is\n  technically correct, gcc is capable of\n  inlining these automatically without\n  help, and the maintenance issue of\n  removing the inline when a second user\n  appears outweighs the potential value\n  of the hint that tells gcc to do\n  something it would have done anyway.</p>\n</blockquote>\n'}, {'answer_id': 60836, 'author': 'Mladen Janković', 'author_id': 6300, 'author_profile': 'https://Stackoverflow.com/users/6300', 'pm_score': 0, 'selected': False, 'text': '<p>Excessive inlining of functions can increase size of compiled executable which can have negative impact on cache performance, but nowadays compiler decide about function inlining on their own (depending on many criterias) and ignore inline keyword. </p>\n'}, {'answer_id': 60850, 'author': 'Alan', 'author_id': 2958, 'author_profile': 'https://Stackoverflow.com/users/2958', 'pm_score': 6, 'selected': True, 'text': "<p>It worth pointing out that the inline keyword is actually just a hint to the compiler. The compiler may ignore the inline and simply generate code for the function someplace.</p>\n\n<p>The main drawback to inline functions is that it can <strong>increase the size of your executable</strong> (depending on the number of instantiations).  This can be a problem on some platforms (eg. embedded systems), especially if the function itself is recursive.</p>\n\n<p>I'd also recommend making inline'd functions <strong>very small</strong> - The speed benefits of inline functions tend to diminish as the function grows in size. At some point the overhead of the function call becomes small compared to the execution of the function body, and the benefit is lost.</p>\n"}, {'answer_id': 60854, 'author': 'Fire Lancer', 'author_id': 6266, 'author_profile': 'https://Stackoverflow.com/users/6266', 'pm_score': 1, 'selected': False, 'text': "<p>You should also note that the inline keyword is only a request. The compiler may choose not to inline it, likewise the compiler may choose to make a function inline that you did not define as inline if it thinks the speed/size tradeoff is worth it.</p>\n\n<p>This decision is generaly made based on a number of things, such as the setting between optimise for speed(avoids the function call) and optimise for size (inlining can cause code bloat, so isn't great for large repeatedly used functions).</p>\n\n<p>with the VC++ compiler you can overide this decision by using <code>__forceinline</code></p>\n\n<p>SO in general:\nUse inline if you really want to have a function in a header, but elsewhere theres little point because if your going to gain anything from it, a good compiler will be making it inline for you anyway.</p>\n"}, {'answer_id': 60941, 'author': 'maccullt', 'author_id': 4945, 'author_profile': 'https://Stackoverflow.com/users/4945', 'pm_score': 2, 'selected': False, 'text': '<p>I agree with the other posts:</p>\n\n<ul>\n<li>inline may be superfluous because the compiler will do it</li>\n<li>inline may bloat your code</li>\n</ul>\n\n<p>A third point is it may force you to expose implementation details in your headers, .e.g.,</p>\n\n<pre><code>class OtherObject;\n\nclass Object {\npublic:\n    void someFunc(OtherObject&amp; otherObj) {\n        otherObj.doIt(); // Yikes requires OtherObj declaration!\n    }\n};\n</code></pre>\n\n<p>Without the inline a forward declaration of OtherObject was all you needed. With the inline your \nheader needs the definition for OtherObject.</p>\n'}, {'answer_id': 61290, 'author': 'Nick', 'author_id': 4949, 'author_profile': 'https://Stackoverflow.com/users/4949', 'pm_score': 1, 'selected': False, 'text': '<p>Inlining larger functions can make the program larger, resulting in more <a href="https://stackoverflow.com/questions/22394750/what-is-meant-by-data-cache-and-instruction-cache">instruction cache</a> misses and making it slower.</p>\n\n<p>Deciding when a function is small enough that inlining will increase performance is quite tricky. <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Inline_Functions" rel="nofollow noreferrer">Google\'s C++ Style Guide</a> recommends only inlining functions of 10 lines or less.</p>\n\n<p>(Simplified) <strong>Example</strong>:</p>\n\n<p>Imagine a simple program that just calls function "X" 5 times.</p>\n\n<p>If X is small and all calls are inlined: Potentially all instructions will be prefetched into the instruction cache with a single main memory access - great!</p>\n\n<p>If X is large, let\'s say approaching the capacity of the instruction cache:<br>\nInlining X will potentially result in fetching instructions from memory once for each inline instance of X.<br>\nIf X isn\'t inlined, instructions may be fetched from memory on the first call to X, but could potentially remain in the cache for subsequent calls.</p>\n'}, {'answer_id': 61299, 'author': 'Niall', 'author_id': 6049, 'author_profile': 'https://Stackoverflow.com/users/6049', 'pm_score': 2, 'selected': False, 'text': "<p>As others have mentioned, the inline keyword is only a hint to the compiler. In actual fact, most modern compilers will completely ignore this hint. The compiler has its own heuristics to decide whether to inline a function, and quite frankly doesn't want your advice, thank you very much.</p>\n\n<p>If you really, really want to make something inline, if you've actually profiled it and looked at the disassembly to ensure that overriding the compiler heuristic actually makes sense, then it is possible:</p>\n\n<ul>\n<li>In VC++, use the __forceinline keyword</li>\n<li>In GCC, use __attribute__((always_inline))</li>\n</ul>\n\n<p>The inline keyword does have a second, valid purpose however - declaring functions in header files but not inside a class definition. The inline keyword is needed to tell the compiler not to generate multiple definitions of the function.</p>\n"}, {'answer_id': 67582, 'author': 'Bronek', 'author_id': 10042, 'author_profile': 'https://Stackoverflow.com/users/10042', 'pm_score': 3, 'selected': False, 'text': '<p>There is a problem with inline - once you defined a function in a header file (which implies inline, either explicit or implicit by defining a body of a member function inside class) there is no simple way to change it without forcing your users to recompile (as opposed to relink). Often this causes problems, especially if the function in question is defined in a library and header is part of its interface.</p>\n'}, {'answer_id': 70094, 'author': 'Stacker', 'author_id': 6574, 'author_profile': 'https://Stackoverflow.com/users/6574', 'pm_score': 2, 'selected': False, 'text': "<p>I don't know if my answer's related to the question but:</p>\n\n<p>Be <strong>very</strong> careful about inline virtual methods! Some buggy compilers (previous versions of Visual C++ for example) would generate inline code for virtual methods where the standard behaviour was to do nothing but go down the inheritance tree and call the appropriate method.</p>\n"}, {'answer_id': 38554646, 'author': 'Ankit-Bhalwal', 'author_id': 6632126, 'author_profile': 'https://Stackoverflow.com/users/6632126', 'pm_score': -1, 'selected': False, 'text': '<ol>\n<li><p>As other people said that inline function can create a problem if the the code is large.As each instruction is stored in a specific memory location ,so overloading of inline function make a code to take more time to get exicuted.</p></li>\n<li><p>there are few other situations where inline may not work</p>\n\n<ol>\n<li>does not work in case of recursive function.</li>\n<li>It may also not work with static variable.</li>\n<li>it also not work in case there is use of a loop,switch etc.or we can         say that with multiple statements.</li>\n<li>And the function main cannot work as inline function.</li>\n</ol></li>\n</ol>\n'}, {'answer_id': 38563033, 'author': 'Tom Tanner', 'author_id': 1182921, 'author_profile': 'https://Stackoverflow.com/users/1182921', 'pm_score': 0, 'selected': False, 'text': "<p>Among other issues with inline functions, which I've seen heavily overused (I've seen inline functions of 500 lines), what you have to be aware of are:</p>\n\n<ul>\n<li><p>build instability</p>\n\n<ul>\n<li>Changing the source of an inline function causes all the users of the header to recompile</li>\n<li><code>#include</code>s leak into the client. This can be very nasty if you rework an inlined function and remove a no-longer used header which some client has relied on.</li>\n</ul></li>\n<li><p>executable size</p>\n\n<ul>\n<li>Every time an inline is inlined instead of a call instruction the compiler has to generate the whole code of the inline. This is OK if the code of the function is short (one or two lines), not so good if the function is long</li>\n<li>Some functions can produce a lot more code than at first appears. I case in point is a 'trivial' destructor of a class that has a lot of non-pod member variables (or two or 3 member variables with rather messy destructors). A call has to be generated for <em>each</em> destructor.</li>\n</ul></li>\n<li><p>execution time</p>\n\n<ul>\n<li>this is very dependent on your CPU cache and shared libraries, but locality of reference is important. If the code you might be inlining happens to be held in cpu cache in one place, a number of clients can find the code an not suffer from a cache miss and the subsequent memory fetch (and worse, should it happen, a disk fetch). Sadly this is one of those cases where you really need to do performance analysis.</li>\n</ul></li>\n</ul>\n\n<p>The coding standard where I work limit inline functions to simple setters/getters, and specifically say destructors should not be inline, unless you have performance measurements to show the inlining confers a noticeable advantage.</p>\n"}, {'answer_id': 69990956, 'author': 'Sergei Ozerov', 'author_id': 7666352, 'author_profile': 'https://Stackoverflow.com/users/7666352', 'pm_score': 0, 'selected': False, 'text': '<p>In addition to other great answers, at least once I saw a case where forced inlining actually slowed down the affected code by 1.5x. There was a nested loop inside (pretty small one) and when this function was compiled as a separate unit, compiler managed to efficiently unroll and optimize it. But when same function was inlined into much bigger outer function, compiler (MSVC 2017) failed to optimize this loop.</p>\n'}]
['https://Stackoverflow.com/questions/60830', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/123/']