Inline Function Advantages

What are the advantages of inline functions over macros?

Questions by chilton

Showing Answers 1 - 18 of 18 Answers

dipuprits

  • Apr 14th, 2009
 

To add to the above points, Inline functions can be overloaded but Macros
cannot be overloaded.


Ex:
#define MYMAC(p) if(p) cout << "single pointer valid" << endl;
#define MYMAC(p,h) if(h==1) cout << "pinter and H valid" << endl;


The second version replaces the macro. So a call to MYMAC(ptr) where ptr is
some pointer, you get warnings that not enough arguments for the macro which is
because the second version has replaced the MYMAC definition.


Inline functions are not always advantageous.


They are appropriate when their impact on code size in not adverse i.e. their size is small and they are not called from too many different places.

They are suitable when they are in the deepest of the loop hierarchies. It helps remove too many fuction calls and improve performance. It may also help in better code optimizations.

The other advantages have been covered above like compiler support for type checking and facility of providing overloads.

  Was this answer useful?  Yes

AnisHasan

  • Jul 16th, 2009
 

Beside the adventages which are mentioned there are some more advantages of Inline Functions over Macros

1> Macro call is resolve at the compile time and compiler replce Identifier with charecter sequence so it is tough to understand error
Ex

#define ASPECT_RATIO 1.653

error message may refer to 1.653, not ASPECT_RATIO.

2> Some time operators get incremented two times in case of macros

#define max(a,b) ((a) > (b) ? (a) : (b))

max(++a, b); // a is incremented twice
((++a)>(b) ? (++a) :(b))


  Was this answer useful?  Yes

ashwin2090

  • Aug 28th, 2009
 

Inline function very much confused to use it or not.
Well I want to just tell you that it has large number of disadvantages than advantages
Inline function works faster than normal function. They are included in the code where the function call appears.
The main disadvantage of inline function is the memory collapse as the function are included every time where the function call appears.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions