Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

According to what I read, the compiler is not obliged to substitute the function call of an inline function with its body, but will do so if it can. This got me thinking- why do we have the inline word if that is the case? Why not make all function inline functions by default and let the compiler figure out if it can substitute the calls with the function body or not?

share|improve this question
    
Here. Page 6 –  EpsilonVector Jan 10 '11 at 18:20
add comment

5 Answers

up vote 29 down vote accepted

inline is from C; it was not new to C++.

There are C keywords (register and inline) that were designed to allow the programmer to assist in code optimization. These are generally ignored nowadays, since compilers can do better at register assignment and deciding when to inline functions (in fact, a compiler can either inline or not inline a function at different times). Code generation on modern processors is far more complicated than on the more deterministic ones common when Ritchie was inventing C.

What the word means now, in C++, is that it can have multiple identical definitions, and needs to be defined in every translation unit that uses it. (In other words, you need to make sure it can be inlined.) You can have an inline function in a header with no problems, and member functions defined in a class definition are automatically effectively inline.

share|improve this answer
    
+1 for the history of inline. –  Tom Wijsman Jan 10 '11 at 18:53
8  
I'm pretty sure that inline was standardized in C++ first, although it was already available as a vendor extension in C.... yep, seems it was added to the C standard in C99. –  Ben Voigt Jan 10 '11 at 22:30
    
@Ben Voigt: You're right. I first encountered it in C. –  David Thornley Jan 11 '11 at 15:16
add comment

Originally inline was a very strong hint that calls to the function should be inlined.

But the only guaranteed effect of inline is to allow a function to be defined (effectively identically) in multiple translation units, e.g., that you place the definition in a header file.

Nowadays, some compilers are very keen on following the inlining hint, e.g. g++. And some compilers take it less seriously, e.g. Visual C++. But all have to abide by the guarantee.

It is unfortunate that these two meanings -- optimization hint and what we might call a linker level discardable definition -- reside with the same keyword, because it means that you cannot practically have one without the other.

It is also unfortunate that inline (or better, a separate keyword about discardable definition) cannot be applied to data.

The need for linker level discardable data has increased as header-only modules have become more popular. E.g., many Boost sub-libraries are header-only.

For data you can, however, apply a little trick with templates. Define it in some class template, provide a typedef with template parameter void (or whatever). That's because the One Definition Rule makes a specific exception for templates.

Cheers & hth.,

share|improve this answer
    
+1 for the extra information about inline. –  Tom Wijsman Jan 10 '11 at 18:53
add comment

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.

share|improve this answer
    
+1 to the cache example! Didn't think of that. –  EpsilonVector Jan 10 '11 at 19:42
1  
But the compiler can (and does!) figure this out much better than the programmer in general. So this is not a valid argument. –  Konrad Rudolph Jan 10 '11 at 20:36
add comment

Let's see what the standard says (highlighted important parts in bold):

2. A function declaration with an inline specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions shall still be respected.

C++ standard, ISO/IEC 14882:2003, 7.1.2 Function Specifiers [dcl.fct.spec]

So, if you want to be sure, you should read the documentation of your compiler.

Inlining everything is a bad idea, as it might result in a lot of duplicated machine code...

So you'll have to know:

There are no simple answers: You have to play with it to see what is best. Do not settle for simplistic answers like, "Never use inline functions" or "Always use inline functions" or "Use inline functions if and only if the function is less than N lines of code." These one-size-fits-all rules may be easy to write down, but they will produce sub-optimal results.

C++ FAQ, Inline Functions, 9.3 Do inline functions improve performance?

share|improve this answer
1  
What you say is true, but not relevant because as the programmer it's not your decision whether to inline or not. You put the keyword, you might or might not get your functions inlined. You don't put the keyword, you still might or might not get them inlined. It's pointless to have ANY rules for when you put the keyword, what with the compiler being the one actually making the decision. –  Kate Gregory Jan 19 '11 at 15:05
    
How isn't this relevant if it says exactly the same as you? There are no simple answers. –  Tom Wijsman Jan 19 '11 at 15:10
    
It's not relevant because it doesn't answer the OPs question. He's not asking "what does inline do", he's asking "whats the point". And your answer just reiterates what we all already know about inline. –  Davor Ždralo Jun 11 '11 at 19:46
    
@Davor: "What's the point?" doesn't adhere to the FAQ, so I'm answering the questions posed within the question. I'm telling the point of inline by reiterating the knowledge as the OP seems to miss a part so which is the core reason why he doesn't get the point. For one to get a point he needs to have an understanding of what's beneath that point... –  Tom Wijsman Jun 11 '11 at 19:50
    
Why the hell wouldn't it adhere to the FAQ? Standard describes syntax and semantics of inline keyword, and OPs question is what is the purpose, when should it be used, what problems is it supposed to solve? I don't see how that doesn't adhere to the FAQ. –  Davor Ždralo Jun 11 '11 at 22:00
add comment

Let me give you a good reason for using the inline keyword.

On an embedded system, such as an ticket printer or similar smaller system. The processor is very limited and a function call (preparing function parameters on stack, call, fetch params from stack and put back answer etc..) can take several ms to execute, beside the function itself.

Lets say the call time is about 60ms (just for calling, not the actual function) and you have do 50 iterations (loop or iterative calls in a tree).

The time to just move forth and back from that function call will take 60 * 50 = 3000 (3 seconds).

If you have the memory you would definitely do an inline to save 3 seconds.

So inline are basically used when you need execution speed. In some projects I was involved with, the calling time was longer than the execution time, a classic situation when to use inline.

share|improve this answer
    
Eh, what? Putting inline in your code doesn't mean that compiler will actually inline it, and not putting it there also doesn't mean that it wont. It's just a hint, which is mostly ignored by todays compilers. If compiler finds it useful to inline, then it will, otherwise, it wont. You have no real control there. OP already knows this, and is asking, i quote: "What's the point?". Who the hell gives +1 on this? It's both misleading (implying that keyword inline enforces inlining) and irrelevant to the question. –  Davor Ždralo Jun 11 '11 at 19:52
    
@Davor Ždralo No need to be rude. I interpreted the question as WHY should I use inlines? My point was to show that you can get the code faster at cost of memory. Each compiler may handle the inline keyword different, so you need to check the documentation, which I didn't mention. Since you can't always afford the extra memory overhead inlines creates, it is useful to "guide" when to use it. I also didn't say that inlines are enforced. Someone found this answer useful to him/her and voted +1, please respect that other may have another level of experience and find something trivial useful. –  Max Kielland Jun 13 '11 at 18:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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