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
int safe(int i)
return i >= 0 ? i : -i;
void userCode(int x)
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.
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.
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.