内联vs宏

问题描述:

嗨伙计们,


你能否建议在什么情况下,宏应该比内联函数优先使用
,反之亦然?是否有任何情况下使用宏的
与内联函数相比会更有效?


感谢您提前提供任何帮助...

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 ...


ju ********** @ yahoo.co.in 写道:

嗨伙计们,


你能否建议在什么情况下宏应该比内联函数更优惠
反之?是否有任何情况下,与内联函数相比,

使用宏会更有效?


感谢提前任何帮助...
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 ...



您可以参考以下链接,

http://groups.google.co.in/group/com...13a697/?hl= en#


并且内联函数比Macro要好得多,如果你看一下上面线程中的例子,你将会理解.b


You could refer to the following link,

http://groups.google.co.in/group/com...13a697/?hl=en#

And inline functions are much better than Macro, which you will
understand if you look at the examples in the above thread.


ju **** ****** @ yahoo.co.in écrit:
ju**********@yahoo.co.in 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)


定义内联函数(s )那将是

相当困难...


同样的分钟(a,b)


其他优点宏是他们可以在调用函数中捕获
局部变量:


#define myhack(a)(a + sqrt(var))


void fn(double var)

{

int a;


.. 。

myhack(a);

...

}


你明白了吗?参数var隐含地通过

到宏。


另一方面:


优点内联函数是他们

不会在调用中捕获局部变量

函数:


内联双myhack(int a)

{

返回+ sqrt(var);

//这个var指的是全局变量var,

//不是局部变量。这避免了

//当地的无意识问题

//变量

}

#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
}


< ju ********** @ yahoo.co.inwrote in message

news:11 ***************** ****@f1g2000cwa.googlegrou ps.com ...
<ju**********@yahoo.co.inwrote in message
news:11*********************@f1g2000cwa.googlegrou 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 ?



如果你可以用另一个替换一个并保留调用者的语法,

没有理由期望一个是比另一个更有效率。一个

编译器应该对它们进行同样的处理。


有时你不能替换宏。考虑一下:


#define INC(x)(x ++)


函数根本无法做同样的事情。您将不得不更改来电者




int x = 0;

INC(x);

到:


int x = 0;

x = inc(x); / *假设int inc(x){return x + 1; } * /


您可能也在调用具有不同类型参数的宏;那里

无法编写一个内联函数来替换上面的INC()可以

正确处理双精度和整数。


S


-

Stephen Sprunk上帝不玩骰子。 --Albert Einstein

CCIE#3723上帝是一个顽固的赌徒,他会在每一个可能的机会投掷
K5SSS骰子。 --Stephen Hawking

-

通过 http://www.teranews.com

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