Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use inline functions? [duplicate]

Tags:

c++

inline

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?


2 Answers

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:

  • http://msdn.microsoft.com/en-us/library/1w2887zk(v=vs.80).aspx
  • http://www.cplusplus.com/forum/articles/20600/
  • http://www.glenmccl.com/bett_007.htm
  • http://www.exforsys.com/tutorials/c-plus-plus/inline-functions.html
like image 108
Rudi Visser Avatar answered Dec 24 '25 12:12

Rudi Visser


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.

like image 44
Diego Sevilla Avatar answered Dec 24 '25 12:12

Diego Sevilla