Possible Duplicate:
When should I write the keyword 'inline' for a function/method?
I have a question about inline functions in c++. I know inline functions are used to replace each apperance with the inline function body. But I do not know when to use it. It is said that inline functions can improve performance, but when should I consider using an inline function?
Inline functions are best for small stubs of code that are performance-critical and reused everywhere, but mundane to write.
However, you could also use them to split up your code, so rather than having a 1000-line long function, you may end up splitting this up into a couple of 100-line long methods. You could inline these so it would have the same effect as a 1000-line long method.
Modern compilers will automatically inline some of your smaller methods, however you can always manually inline ones that are bigger.
There are many resources about this, for example:
Usually modern compilers are intelligent enough to inline certain small functions to a limit (in increment of space). You can provide the hint. Of course, you'll inline small, repetitive functions that save the overhead of the call.
Note also, that sometimes, even when you provide the inline keyword, the compiler can decide not to inline that function, so its use (for optimization purposes, at least) is somewhat informative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With