1.11M Members

Diff betn Macros and Inline

 
0
 

Hi All,

What all the difference between Macros and Inline functions. Why do they prefer inline functions? When Do macros come in use?

What are the disadvantages of Macros and inline functions?

thanks in advance

 
2
 

When the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream (conceptually similar to what happens with a #define macro). This can, depending on a zillion other things, improve performance, because the optimizer can procedurally integrate the called code — optimize the called code into the caller.

There are several ways to designate that a function is inline, some of which involve the inline keyword, others do not. No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: it might inline-expand some, all, or none of the calls to an inline function. (Don't get discouraged if that seems hopelessly vague. The flexibility of the above is actually a huge advantage: it lets the compiler treat large functions differently from small ones, plus it lets the compiler generate code that is easy to debug if you select the right compiler options.)


Unlike #define macros, inline functions avoid infamous macro errors since inline functions always evaluate every argument exactly once. In other words, invoking an inline function is semantically just like invoking a regular function, only faster:

// A macro that returns the absolute value of i
 #define unsafe(i)  \
         ( (i) >= 0 ? (i) : -(i) )
 
 // An inline function that returns the absolute value of i
 inline
 int safe(int i)
 {
   return i >= 0 ? i : -i;
 }
 
 int f();
 
 void userCode(int x)
 {
   int ans;
 
   ans = unsafe(x++);   // Error! x is incremented twice
   ans = unsafe(f());   // Danger! f() is called twice
 
   ans = safe(x++);     // Correct! x is incremented once
   ans = safe(f());     // Correct! f() is called once
 }

Also unlike macros, argument types are checked, and necessary conversions are performed correctly.


All this information has been copied verbatim from Marshall Clines C++ FAQ Lite which is available online.

http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5

Go there for more info...

 
-1
 

Thanks to both of you for your inputs.
I just wanted to know, are there any advantages of having Macros over Inline? If there are any, please mention.

many thanks.

 
0
 

Thanks to both of you for your inputs.
I just wanted to know, are there any advantages of having Macros over Inline? If there are any, please mention.

many thanks.

Check out the link to Marshall Clines C++ FAQ. He will tell you most of the situations in which macros are useful. When you get to the FAQ use its search facility and search for 'macro', funnily enough.

Question Answered as of 4 Years Ago by mattjbond and preetg
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article