274

What is the advantages/disadvantages of using inline functions in C++? I see that it only increases performance for the code that the compiler outputs, but with today's optimized compilers, fast CPUs, huge memory etc. (not like in the 1980< where memory was scarce and everything had to fit in 100KB of memory) what advantages do they really have today?

4
  • 52
    This is one of those questions where common knowledge is wrong. Everybody has answered with the standard Comp Sci answer. (Inlining saves function call costs but increases code size). Rubbish. It provides a simple mechanism for the compiler to apply more OPTIMIZATIONS. Oct 1, 2008 at 16:12
  • 37
    This is one of those answers posing as comments. If you don't like any of the answers that were posted, post your own answer and see how it goes. Mar 2, 2010 at 14:18
  • 11
    The basis of this question is flawed. C++ inline functions have little to do with compilers inlining during compilation. It is unfortunate that inline is a c++ keyword and that inlining is a compiler optimization technique. See this question "when should I write the keyword inline for a function/method" for the correct answer.
    – deft_code
    Mar 24, 2011 at 22:37
  • 3
    @JoseVega Your link got mangled - the current link is exforsys.com/tutorials/c-plus-plus/inline-functions.html
    – user146043
    May 1, 2014 at 12:25

14 Answers 14

209

Advantages

  • By inlining your code where it is needed, your program will spend less time in the function call and return parts. It is supposed to make your code go faster, even as it goes larger (see below). Inlining trivial accessors could be an example of effective inlining.
  • By marking it as inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining)

Disadvantages

  • It can make your code larger (i.e. if you use inline for non-trivial functions). As such, it could provoke paging and defeat optimizations from the compiler.
  • It slightly breaks your encapsulation because it exposes the internal of your object processing (but then, every "private" member would, too). This means you must not use inlining in a PImpl pattern.
  • It slightly breaks your encapsulation 2: C++ inlining is resolved at compile time. Which means that should you change the code of the inlined function, you would need to recompile all the code using it to be sure it will be updated (for the same reason, I avoid default values for function parameters)
  • When used in a header, it makes your header file larger, and thus, will dilute interesting informations (like the list of a class methods) with code the user don't care about (this is the reason that I declare inlined functions inside a class, but will define it in an header after the class body, and never inside the class body).

Inlining Magic

  • The compiler may or may not inline the functions you marked as inline; it may also decide to inline functions not marked as inline at compilation or linking time.
  • Inline works like a copy/paste controlled by the compiler, which is quite different from a pre-processor macro: The macro will be forcibly inlined, will pollute all the namespaces and code, won't be easily debuggable, and will be done even if the compiler would have ruled it as inefficient.
  • Every method of a class defined inside the body of the class itself is considered as "inlined" (even if the compiler can still decide to not inline it
  • Virtual methods are not supposed to be inlinable. Still, sometimes, when the compiler can know for sure the type of the object (i.e. the object was declared and constructed inside the same function body), even a virtual function will be inlined because the compiler knows exactly the type of the object.
  • Template methods/functions are not always inlined (their presence in an header will not make them automatically inline).
  • The next step after "inline" is template metaprograming . I.e. By "inlining" your code at compile time, sometimes, the compiler can deduce the final result of a function... So a complex algorithm can sometimes be reduced to a kind of return 42 ; statement. This is for me extreme inlining. It happens rarely in real life, it makes compilation time longer, will not bloat your code, and will make your code faster. But like the grail, don't try to apply it everywhere because most processing cannot be resolved this way... Still, this is cool anyway...
    :-p
5
  • you said It slightly breaks your encapsulation. Will you please explain it using an example?
    – Destructor
    Sep 6, 2015 at 13:59
  • 6
    @PravasiMeet : It's C++. Let's say you deliver a DLL/shared library to a client, who compiles against it. The inline function foo, using member variable X and doing work Y will get inlined in the client's code. Let's say to need to deliver an updated version of your DLL where you changed the member variable to Z, and add a work YY in addition to work Y. The client just copies the DLL into their project, and BOOM, because the code of foo in their binary is not the updated code you wrote... Despite the client not having legal access to your private code, inlining makes it quite "public".
    – paercebal
    Sep 7, 2015 at 8:38
  • @paercebal Regarding your second to last bullet point, can you give an example of when a function template is not inline? I thought they were always inline, though I don’t have a reference handy now (a simple test seems to confirm it though). Jun 22, 2016 at 9:37
  • @KonradRudolph In n4594 I see: 3.2/6: There can be more than one definition of [..] inline function with external linkage [..] non-static function template. At 5.1.5/6 For a generic lambda, the closure type has a public inline function call operator member template. And at 7.1.2/2: the use of inline keyword is to declare an inline function where it is a suggestion to inline the function body at point of call. So, I conclude that even if they can behave the same, inline functions and function templates are still separate, orthogonal notions that can be mixed (ie inline function template)
    – paercebal
    Jul 10, 2016 at 10:08
  • encapsulation is broken? How so? encapsulation is for the guy(s) programming, not for the actual objects in memory. at that point no one cares. Even if you distribute a library, the compiler may choose to inline or not to do so on it's own. so in the end when you get a new lib, you just have to recompile everything that uses the functions and objects from that library.
    – FalcoGer
    Apr 2, 2019 at 6:36
150

Inline functions are faster because you don't need to push and pop things on/off the stack like parameters and the return address; however, it does make your binary slightly larger.

Does it make a significant difference? Not noticeably enough on modern hardware for most. But it can make a difference, which is enough for some people.

Marking something inline does not give you a guarantee that it will be inline. It's just a suggestion to the compiler. Sometimes it's not possible such as when you have a virtual function, or when there is recursion involved. And sometimes the compiler just chooses not to use it.

I could see a situation like this making a detectable difference:

inline int aplusb_pow2(int a, int b) {
  return (a + b)*(a + b) ;
}

for(int a = 0; a < 900000; ++a)
    for(int b = 0; b < 900000; ++b)
        aplusb_pow2(a, b);
7
  • 27
    As I suspected inlining makes no difference to the above. Compiled with gcc 4.01. Version 1 forced to use inlining: 48.318u 1.042s 5:51.39 99.4% 0+0k 0+0io 0pf+0w Version 2 forced no inlining 348.311u 1.019s 5:52.31 99.1% 0+0k 0+0io 0pf+0w This is a good example were common knowledge is wrong. Sep 28, 2008 at 20:38
  • 38
    while the call itself indeed matters, that's only the minor gain you get by using inline. The major gain is, that the compiler now see where pointers don't alias each other, where variables of the caller end up in the callee and so on. Thus, the following optimization is what matters more. Nov 10, 2008 at 8:04
  • 31
    It probably doesn't make a difference in this snippet since the result of the function is never used nor does the function have side effects. We see a measurable performance gain in inlining in image processing.
    – plinth
    Mar 2, 2010 at 14:28
  • 4
    The reason for no difference could be that the compiler might inline on its own accord; or that the code is small so there are no code prefetch issues.
    – einpoklum
    Dec 19, 2015 at 16:15
  • 3
    @einpoklum The compiler may have even optimized out the entire loop because of that. Jun 13, 2016 at 16:51
47

In archaic C and C++, inline is like register: a suggestion (nothing more than a suggestion) to the compiler about a possible optimization.

In modern C++, inline tells the linker that, if multiple definitions (not declarations) are found in different translation units, they are all the same, and the linker can freely keep one and discard all the other ones.

inline is mandatory if a function (no matter how complex or "linear") is defined in a header file, to allow multiple sources to include it without getting a "multiple definition" error by the linker.

Member functions defined inside a class are "inline" by default, as are template functions (in contrast to global functions).

//fileA.h
inline void afunc()
{ std::cout << "this is afunc" << std::endl; }

//file1.cpp
#include "fileA.h"
void acall()
{ afunc(); }

//main.cpp
#include "fileA.h"
void acall();

int main()
{ 
   afunc(); 
   acall();
}

//output
this is afunc
this is afunc

Note the inclusion of fileA.h into two .cpp files, resulting in two instances of afunc(). The linker will discard one of them. If no inline is specified, the linker will complain.

17

Inlining is a suggestion to the compiler which it is free to ignore. It's ideal for small bits of code.

If your function is inlined, it's basically inserted in the code where the function call is made to it, rather than actually calling a separate function. This can assist with speed as you don't have to do the actual call.

It also assists CPUs with pipelining as they don't have to reload the pipeline with new instructions caused by a call.

The only disadvantage is possible increased binary size but, as long as the functions are small, this won't matter too much.

I tend to leave these sorts of decisions to the compilers nowadays (well, the smart ones anyway). The people who wrote them tend to have far more detailed knowledge of the underlying architectures.

2
  • More importantly, optimizing the function body into the body of the caller allows constant-propagation and other optimizations. The "reload the pipeline" is usually an over-statement: unconditional direct branches tend to be well predicted, although a taken branch isn't free even if predicted correctly. But setup of args, and having to use call-preserved registers for things that must survive across a non-inlined call, tend to be more expensive. Aug 24, 2022 at 21:18
  • Leaving it to the compiler works well if you enable LTO (link-time optimization, like gcc -flto -O3), to allow inlining across C / C++ source files, without having to put functions in .h headers. Otherwise it can only work within one compilation unit. Aug 24, 2022 at 21:19
14

Inline function is the optimization technique used by the compilers. One can simply prepend inline keyword to function prototype to make a function inline. Inline function instruct compiler to insert complete body of the function wherever that function got used in code.

Advantages :-

  1. It does not require function calling overhead.

  2. It also save overhead of variables push/pop on the stack, while function calling.

  3. It also save overhead of return call from a function.

  4. It increases locality of reference by utilizing instruction cache.

  5. After in-lining compiler can also apply intra-procedural optimization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..

To check more about it one can follow this link http://tajendrasengar.blogspot.com/2010/03/what-is-inline-function-in-cc.html

2
  • 4
    1) It's a suggestion, not an instruction 2) It can cause more cache misses because of the increase in code size if a commonly used function is inlined a lot
    – Flexo
    Sep 14, 2011 at 18:09
  • 3
    Is the link at the end your personal blog? If it is you should declare it as such, otherwise it looks like spam.
    – Flexo
    Sep 14, 2011 at 18:10
6

I'd like to add that inline functions are crucial when you are building shared library. Without marking function inline, it will be exported into the library in the binary form. It will be also present in the symbols table, if exported. On the other side, inlined functions are not exported, neither to the library binaries nor to the symbols table.

It may be critical when library is intended to be loaded at runtime. It may also hit binary-compatible-aware libraries. In such cases don't use inline.

2
  • @Johnsyweb: read my answer carefully. What you have said is true, when you are building an executable. But compiler can not simply ignore inline when building a shared library!
    – mip
    May 20, 2010 at 6:50
  • static functions, or ELF __attribute__((visibility("hidden"))), are also not exported in the shared library. Aug 24, 2022 at 21:34
4

During optimization many compilers will inline functions even if you didn't mark them. You generally only need to mark functions as inline if you know something the compiler doesn't, as it can usually make the correct decision itself.

2
  • Many compilers also won't do this, MSVC for example will not do this unless you tell it to
    – paulm
    Jan 2, 2014 at 15:23
  • @paulm I'd argue that if you didn't tell your compiler to inline functions if it would be more efficient (which in my experience for many compilers is implied by just tell ing the compiler to optimize the code) then you didn't really need or want inlined functions enough for the absence of inline in the source to be a big deal. And if a basic "optimize this well" option doesn't tell the compiler to inline functions where best, it's just not a good compiler (by modern standards anyway).
    – mtraceur
    Aug 26, 2022 at 8:49
4

inline allows you to place a function definition in a header file and #include that header file in multiple source files without violating the one definition rule.

16
  • For the record, so does static, and static predates inline and is more directly defined to mean "this is not visible outside of this translation unit".
    – mtraceur
    Sep 25, 2020 at 20:40
  • @mtraceur: True, but static makes a separate copy of the machine code for each compilation unit if the compiler chooses not to inline it at every call site in that compilation unit. With inline, those duplicate definitions gets merged by the linker. So inline is much better for functions that might not get inlined. (Smaller code footprint). Aug 24, 2022 at 21:36
  • Hmm...yes, though that difference seems easy to optimize out, so much so that I think it vanishes in practice soon enough (if you're doing whole-program optimization without throwing out information between steps, it's trivial) (if you do something like having the compiler generate the to-be-linked object file with a symbol name for that function's machine code and some annotation for that symbol telling the linker that it was inline at source level, just a little more annotation+logic would let you do it for static functions too).
    – mtraceur
    Aug 26, 2022 at 6:48
  • I guess the big challenge for optimizing static the same as inline in this regard, and what inline really adds semantically, is that a static function with the same name is allowed to have a different definition between translation units. So if you have two linked objects with inline (but not static) function named foo, then as I understand it, the linker is allowed to just assume that they have the same definition... whereas with static (and no inline) the linker can't assume they're the same and must also compare the machine code or some additional metadata.
    – mtraceur
    Aug 26, 2022 at 6:56
  • I would expect future compilers+linkers, and maybe even already modern ones that do link-time/whole-program optimization, to be capable of optimizations like identifying when two pieces of machine code (even without symbol names or special metadata) are identical and could be optimized by deduplicating. Optimizations always march on, and tend to sweep up any code whose semantics are conducive to such optimizations - static always had great semantics to enable all these optimizations. But yes, inline's semantics do help make some of these optimizations simpler to implement.
    – mtraceur
    Aug 26, 2022 at 7:07
3

Generally speaking, these days with any modern compiler worrying about inlining anything is pretty much a waste of time. The compiler should actually optimize all of these considerations for you through its own analysis of the code and your specification of the optimization flags passed to the compiler. If you care about speed, tell the compiler to optimize for speed. If you care about space, tell the compiler to optimize for space. As another answer alluded to, a decent compiler will even inline automatically if it really makes sense.

Also, as others have stated, using inline does not guarantee inline of anything. If you want to guarantee it, you will have to define a macro instead of an inline function to do it.

When to inline and/or define a macro to force inclusion? - Only when you have a demonstrated and necessary proven increase in speed for a critical section of code that is known to have an affect on the overall performance of the application.

2
  • …If you care about space, tell the compiler to optimize for space -- telling the compiler to optimize for speed can result in smaller binaries with C++ and C. similarly, telling the compiler to optimize for space can result in faster execution. iow, these features don't always work as advertised. humans have the ability to understand some aspects of their their program better than a compiler's generalized interpretations (which must also remain usably fast). human intervention doesn't have to be a bad thing.
    – justin
    Jul 15, 2013 at 21:58
  • You need to enable link-time optimization (like gcc -O3 -flto) if you want the compiler to be able to inline small functions across .cpp files, not just within one compilation unit. Otherwise you do need to put small hot functions in .h files (like trivial accessors), and therefore use inline. Aug 24, 2022 at 21:31
3

It is not all about performance. Both C++ and C are used for embedded programming, sitting on top of hardware. If you would, for example, write an interrupt handler, you need to make sure that the code can be executed at once, without additional registers and/or memory pages being being swapped. That is when inline comes in handy. Good compilers do some "inlining" themselves when speed is needed, but "inline" compels them.

0
1

Fell into the same trouble with inlining functions into so libraries. It seems that inlined functions are not compiled into the library. as a result the linker puts out a "undefined reference" error, if a executable wants to use the inlined function of the library. (happened to me compiling Qt source with gcc 4.5.

1

Why not make all functions inline by default? Because it's an engineering trade off. There are at least two types of "optimization": speeding up the program and reducing the size (memory footprint) of the program. Inlining generally speeds things up. It gets rid of the function call overhead, avoiding pushing then pulling parameters from the stack. However, it also makes the memory footprint of the program bigger, because every function call must now be replaced with the full code of the function. To make things even more complicated, remember that the CPU stores frequently used chunks of memory in a cache on the CPU for ultra-rapid access. If you make the program's memory image big enough, your program won't be able to use the cache efficiently, and in the worst case inlining could actually slow your program down. To some extent the compiler can calculate what the trade offs are, and may be able to make better decisions than you can, just looking at the source code.

0

Our computer science professor urged us to never use inline in a c++ program. When asked why, he kindly explained to us that modern compilers should detect when to use inline automatically.

So yes, the inline can be an optimization technique to be used wherever possible, but apparently this is something that is already done for you whenever it's possible to inline a function anyways.

1
  • 6
    Your professor is unfortunately completely wrong. inline in C++ has two very distinct meanings — only one of those is related to optimisation, and your professor is correct with regards to that. However, the second meaning of inline is often necessary to satisfy the One Definition Rule. Jun 22, 2016 at 9:39
-1

Conclusion from another discussion here:

Are there any drawbacks with inline functions?

Apparently, There is nothing wrong with using inline functions.

But it is worth noting the following points!

  • 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. - Google Guidelines

  • 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 - Source

  • There are few situations where an inline function may not work:

    • For a function returning values; if a return statement exists.
    • For a function not returning any values; if a loop, switch or goto statement exists.
    • If a function is recursive. -Source
  • The __inline keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not __inline 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 __inline keyword to be ignored. -Source

4
  • 3
    Would be true if the inline was a command and not a hint to the compiler. Compiler actually decides what to inline. Sep 28, 2008 at 21:09
  • 1
    @LokiAstari I know that inline is request to compiler. My argument is If its hint to the compiler we should leave it to the compiler to decide what is best. Why to use inline any way even if you use inline it's still the compiler who will take final decision. I aslo wonder my Microsoft have introduced _forceinline. Feb 12, 2014 at 6:44
  • @krish_oza: My comment here. Is about this answer. The answer here is completely wrong. Because the compiler disregards the inline keyword for determining whether or not to inline code all the points made above are wrong. They would be true if the compiler used the keyword for inlining (it is only used for determining marking multiple definitions for linking purposes). Feb 12, 2014 at 21:20
  • A return statement is only an obstacle to manually copy/pasting the function body into another function! Not for the compiler inlining. The link supporting to weird claims is dead. A function returning a value must have a return statement in C++; if execution reaches the end of a function without a return, that's undefined behaviour. So the only way to call a non-void function without a return statement would be if the function actually doesn't return, e.g. calling a noreturn function. Some of the other sections on this answer are generally true. Aug 24, 2022 at 21:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.