Re: inline vs macro
- From: jacob navia <jacob@xxxxxxxxxxxxxxxx>
- Date: Tue, 19 Dec 2006 16:21:20 +0100
junky_fellow@xxxxxxxxxxx a écrit :
hi guys,
Can you please suggest that in what cases should a macro be
preferred over inline function and viceversa ? Is there any case where
using a macro will be more efficient as compared to inline function ?
thanks for any help in advance ...
#define max(a,b) ((a<b)?b:a)
Defining an inline function(s) for that would be
quite difficult...
The same for min(a,b)
Other "advantages" of macros is that they can capture
local variables in the calling function:
#define myhack(a) (a+sqrt(var))
void fn(double var)
{
int a;
...
myhack(a);
...
}
You see? The argument var is implicitely passed
to the macro.
On the other hand:
An advantage of inline functions is that they
do not capture local variables in the calling
function:
inline double myhack(int a)
{
return a+sqrt(var);
// This "var" refers to a global variable var,
// NOT to a local variable. This avoids
// UNINTENTIONAL problems with local
// variables
}
.
- Follow-Ups:
- Re: inline vs macro
- From: Keith Thompson
- Re: inline vs macro
- References:
- inline vs macro
- From: junky_fellow@xxxxxxxxxxx
- inline vs macro
- Prev by Date: Re: what malloc(0) should returns?
- Next by Date: Re: size of a typedef with arrays of unsigned char
- Previous by thread: Re: inline vs macro
- Next by thread: Re: inline vs macro
- Index(es):
Relevant Pages
|
|