0

I know that inline functions are used so that we can be rid of function calls.

I see that using inline function is a good way of using the storage and keeping the stack more empty.

Is it a good programming practice to use inline functions everywhere.

I think so, because since compilers are not obligated to use inline functions as inline when the functions are complex, so would it be a nice approach to append that little word inline to every functions?

cipher
  • 2,414
  • 4
  • 30
  • 54
  • 3
    You missed one drawback: the client code has a compile time dependency on the implementation of inline functions. You cannot change the function definition without forcing clients to recompile. – juanchopanza Oct 11 '13 at 12:40
  • 2
    Since compilers can also make functions inline when they are not declared inline you could just as easily argue that you should never use inline. The important point is that despite appearances the keyword `inline` has nothing to do with whether a function will end up being inlined or not. – john Oct 11 '13 at 12:41
  • Possible duplicate : http://stackoverflow.com/questions/145838/benefits-of-inline-functions-in-c?rq=1 – Co_42 Oct 11 '13 at 12:42
  • 2
    @cipher, yes really, with Microsoft's compiler it's called Whole Program Optimization, it takes place during the link phase, and it can inline functions you haven't declared as inline. I believe g++ has a similar feature. – john Oct 11 '13 at 12:43
  • @Nawaz : what is it then. can you explain? – cipher Oct 11 '13 at 12:45
  • @juanchopanza : Didn't get you. When did we not had our code recompiled after the change of function definition? – cipher Oct 11 '13 at 12:48
  • @cipher To understand inline you have to think about when it makes a difference to whether code is legal or not. – john Oct 11 '13 at 12:49
  • 1
    @cipher when the definition is in the implementation, compiled into an object file or library. – juanchopanza Oct 11 '13 at 12:52
  • @john Is the *legal* code you are referring to is, "**code which are not too complex to be inline**" ? – cipher Oct 11 '13 at 12:53
  • No, it's simpler than that. Put a function without inline in a header file, include that header file in more than one source file and you will get multiple definition errors from the linker. The only true meaning of the inline keyword in C++ is to provide an exception to the One Definition Rule. – john Oct 11 '13 at 13:00

2 Answers2

1

My general rule is to use inline functions when the content of the function is less than or equal to the overhead of calling the function.

An example is a getter or setter method.

After the program works correctly and robustly, I convert some functions in the bottleneck sections to use inline. However, this is a micro-optimization and usually doesn't generate as much savings as redesigning an algorithm.

As others have stated, inline functions wreak havoc on the build process because a header file is changed and all dependencies on the header file must be rebuilt. A non-inlined function would only require building of a single translation unit.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

inline is only a hint to the compiler to inline the function. Compilers can also inline other functions you didn't mark as inline when optimizing. The problem with making every function inline, is the whole function will need to be in a header so that it can be called from multiple source files. This will increase compile times. My advice is to forget the inline keyword exists, until you have a real performance issue.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91