<> wrote in message
news: ps.com...
> Can you please suggest that in what cases should a macro be
> preferred over inline function and viceversa ?
An inline function has type safety and doesn't suffer from
double-evaluation problems.
A macro has type generic-ness and can modify its arguments.
Only one of them may meet your needs in particular cases, though in the
simplest examples they may both work equally well.
> Is there any case where using a macro will be more efficient as
> compared to inline function ?
If you can replace one with the other and preserve the caller's syntax,
there is no reason to expect one to be more efficient than the other. A
compiler should treat them the same.
Sometimes you can't replace a macro, though. Consider:
#define INC(x) (x++)
A function simply can't do the same thing. You'd have to change callers
from this:
int x=0;
INC(x);
to:
int x=0;
x = inc(x); /* assumes int inc(x) { return x+1; } */
You might also be calling the macro with varying type arguments; there
is no way to write an inline function to replace INC() above that can
handle both doubles and ints correctly.
S
--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from
http://www.teranews.com