Public Group

# Inline functions

This topic is 4455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been reading up on these... and I'm still unclear as to what purpose they serve... Could anyone tell me what are the advantages making functions inline... Is it just to conserve space and make for neater code? Or are there other useful aspects?

##### Share on other sites
the advantage is, that you save a function call, cause the preproccesor copies the line of codes. you can also but all the function in your header file, but for cleaness you can use the inline statement.

the problem is, that compiler often ignores inline statements.
and the size of your exection file will increas.

##### Share on other sites
Quote:
 the advantage is, that you save a function call, cause the preproccesor copies the line of codes.

No. The compiler will insert the appropriate generated assembly at the call site if a function is inlined. The preprocessor is a vastly different beast and is not involved in any way.

"inline" is a suggestion to the compiler to inline a function. The compiler is free to ignore that suggestion. There are other, implementation-specific keywords that provide a more forceful suggestion to the compiler (such as MSVC's __forceinline), but even those can be ignored under certain circumstances.

Note that modern compilers do a pretty good job of inlining what is actually important to inline, and that inlining does increase the size of the generated object file slightly.

##### Share on other sites
Quote:
 Original post by Chickithe advantage is, that you save a function call, cause the preproccesor copies the line of codes.the problem is, that compiler often ignores inline statements.and the size of your exection file will increas.

It is nothing to do with the pre-processor, inlined functions are a compile time detail. But that aside, yes inline functions essentially cause your function call code to be replaced by the body of the function. In practice it isn't that simple as Chicki said as placing "inline" before a definition is merely a request to the compiler, and it may ignore it if it deems there to be no benefit from inlining it (due to the function being too large for example). There is however the __forceinline specifier which you can use in visual studio at least (not sure about other compilers) to definitely guarantee inlining. Generally however... the compiler knows best, so let it work it out :)

##### Share on other sites
Soo.... More or less, I should just leave out inline functions and let the compiler deal with that until I become more experienced with how computer code works?

##### Share on other sites
Quote:
 Original post by SteveosaurousSoo.... More or less, I should just leave out inline functions and let the compiler deal with that until I become more experienced with how computer code works?
More or less.. You won't go wrong by never inlining a function. As people have said, the compiler can generally do a good job of handling optimisations for you. Premature optimisation is bad, and it is unlikely you will have to worry about this sort of thing. That said, inline can be useful: just don't worry about it too much.

##### Share on other sites
Quote:
 Original post by SteveosaurousSoo.... More or less, I should just leave out inline functions and let the compiler deal with that until I become more experienced with how computer code works?

Well, yes and no.

The compiler will inline functions for you, but only if it can see the definition of the function.

For example:

main.cpp
#include "func.h"int main() {    func1();    func2();}

func.h
void func1();inline void func2() {// actual function body here}

func.cpp
#include "func.h"void func1() {// actual function body here}

When compiling main.cpp, func2 can be inlined because the definition is visable to the compiler. However, func1 only has a declaration, the definition is in func.cpp, so it can not be inlined in main.

So why did we need the inline keyword for func2 if the compiler may inline it for us anyway? Because it prevents a linker error. Since func.h is included into multiple source files, func2 is actually defined in both files. However, you are only allowed to define a function once, and a multiple definition error will be generated at link time. With the inline keyword, even if the compiler chooses not to inline, the linker error is avoided.

When working with classes in C++, any member function definition that appears inside the class declaration is treated as if it has the inline keyword. This is why you don't get a linker error when you do this:

class foo {void func() {// do something}};

And include it into multiple source files.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 77
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009586
• ### Who's Online (See full list)

There are no registered users currently online

×