Why we use inline function?

Recommended Answers

All 6 Replies

INLINE FUNCTIONS DON'T USE STACK. THEREFORE, EXECUTION TIME IS LESS.HOWEVER, THE STATEMENTS IN THE FUNCTION ARE SUBSTITUTED WHENEVER THE FUNCTION IS CALLED.INLINE FUNCTIONS ARE ALWAYS SHORT.

INLINE FUNCTIONS DON'T USE STACK. THEREFORE, EXECUTION TIME IS LESS.HOWEVER, THE STATEMENTS IN THE FUNCTION ARE SUBSTITUTED WHENEVER THE FUNCTION IS CALLED.INLINE FUNCTIONS ARE ALWAYS SHORT.

Many assumptions there, tamilselvi, and none of them are safe: http://www.gotw.ca/gotw/033.htm

Microsoft IDE can generate some pretty huge inline functions which consume a lot of stack space.

"STATEMENTS IN THE FUNCTION ARE SUBSTITUTED WHENEVER THE FUNCTION IS CALLED."

This is very true, but the only thing on the stack would be automatic variables in the function. The function itself is turned into machine (or intermediate) code when compiled. It can expand the size of the translation unit's object code significantly, but you eliminate the overhead of a function call, including all the stack pushing and popping that goes on. In any case, these functions can be very large, but for best effect, keep them as small as possible. This is why in C++ classes, inline methods are generally small getter/setter or other trivial functions. Others should be implemented in the source file instead of the class definition header file. This is a trade off between speed and size. If the function is large and/or complex, then the function call overhead is probably only a small part of the total time taken to execute it.

One final thing, statements are inserted into the function when compiled, not when it is called. :-)

Also, inlin is optional which means the compiler can choose to ignore it if the compiler wants to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.