奇怪的MACRO问题 [英] Strange MACRO Problem

查看:66
本文介绍了奇怪的MACRO问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的宏不起作用?


#define removebrace(x)x


void Foo(int a,int b, char * txt,int d,int e);


main()

{

...

Foo(1,2,removebrace((hello,5,6)));


...

}


我希望编译器扩展后。


Foo(1,2," hello",5,6);


但它没有用。当我看到反汇编时,只有3个参数被推入堆栈,指向字符串hello,值2和值1之前

调用函数''Foo' ',而不是所有5个参数。


但是,如果我不使用宏''removebrace'',那么所有五个参数都会被推送到堆栈中在调用函数''Foo'之前,它工作正常。


任何帮助,为什么MACRO removebrace不起作用。我使用MSVC 6.


有趣的是,这个宏在以下声明中工作正常


p = q * removebrace(a + b)


导致


p =(q * a)+ b


Karim


我以前的帖子似乎丢失了所以再次发布。对不起,如果

你收到两份副本

Why following macro does not work?

#define removebrace(x) x

void Foo(int a, int b, char *txt, int d, int e);

main()
{
...
Foo(1, 2, removebrace(("hello", 5, 6)) );

...
}

I expected following expansion by compiler.

Foo(1, 2, "hello", 5, 6);

But it didnt work. When I see disassembly, only 3 arguments are pushed
into the stack, pointer to string "hello", value 2 and value 1 before
calling function ''Foo'', instead of all 5 arguments.

However, if I do not use macro ''removebrace'', all five arguments get
pushed to the stack before calling function ''Foo'', and it works fine.

Any help, why MACRO removebrace does not work. I use MSVC 6.

Interestingly, this macro works fine in following statement

p = q * removebrace(a+b)

which results in

p = (q*a) + b

Karim

My previous post appeared to be lost so posting it again. Excuse me if
you receive two copy

推荐答案



2004年1月18日星期一,Karim Thapa写道:

On Mon, 18 Jan 2004, Karim Thapa wrote:

为什么下面的宏不起作用?

#define removebrace(x)x


换句话说,''removebrace''几乎 - 但不完全 - 是一个无操作。

特别是,假设''foo''不是#defined标识符,


removebrace(foo)==> foo


for all''foo''。

void Foo(int a,int b,char * txt,int d,int e);

main()
{
..
Foo(1,2,removebrace((hello,5,6)));


由于removebrace((hello,5,6))扩展为(hello,5,6),这个

整行扩展


Foo(1,2,(你好,5,6));


这与原型,所以编译器应该给你

a诊断错误信息。

我希望编译后扩展。

Foo(1,2," ;你好,5,6);


为什么地球上你会期望* * *

但它没有用。当我看到反汇编时,在调用函数''Foo''之前,只有3个参数被推入堆栈,指向字符串hello,值2和值1,而不是所有5个参数。


这是因为你的编译器有足够的常量表达式优化
。它知道,因为你好 5没有副作用,

它们可以简单地丢弃。

但是,如果我不使用宏''removebrace''
[并删除两对额外的括号],所有五个参数在调用函数''Foo'之前被推送到堆栈,并且它工作正常。


自然。

任何帮助,为什么MACRO removebrace不起作用。我使用的是MSVC 6.


确实有效。它用x替换removebrace(x)。

有趣的是,这个宏在以下语句中工作正常

p = q * removebrace(a + b)

p =(q * a)+ b

Why following macro does not work?

#define removebrace(x) x
In other words, ''removebrace'' is almost -- but not quite -- a no-op.
In particular, assuming ''foo'' is not a #defined identifier,

removebrace(foo) ==> foo

for all ''foo''.
void Foo(int a, int b, char *txt, int d, int e);

main()
{
..
Foo(1, 2, removebrace(("hello", 5, 6)) );
Since removebrace(("hello", 5, 6)) expands to ("hello, 5, 6), this
whole line expands to

Foo(1, 2, ("hello", 5, 6) );

This doesn''t match the prototype, so the compiler should give you
a diagnostic error message.
I expected following expansion by compiler.

Foo(1, 2, "hello", 5, 6);
Why on earth would you expect *that*?
But it didnt work. When I see disassembly, only 3 arguments are pushed
into the stack, pointer to string "hello", value 2 and value 1 before
calling function ''Foo'', instead of all 5 arguments.
That''s because your compiler has adequate constant-expression
optimization. It knows that since "hello" and 5 have no side-effects,
they can be simply discarded.
However, if I do not use macro ''removebrace'' [and remove both pairs of the extra parentheses] , all five arguments get
pushed to the stack before calling function ''Foo'', and it works fine.
Naturally.
Any help, why MACRO removebrace does not work. I use MSVC 6.
It does work. It replaces removebrace(x) by x.
Interestingly, this macro works fine in following statement

p = q * removebrace(a+b)

which results in

p = (q*a) + b




不,它导致

p = q * a + b


具有相同的效果;但是你已经在他们不属于的地方插入了一对

括号。


没有C宏''foo''这样的''foo((bar))''替换为''bar''

一般所有文本''bar'',如果这就是你要找的东西。

你真的*试着做什么,为什么?


-Arthur



No, it results in

p = q * a+b

which has the same effect; but you''ve inserted a pair of
parentheses where they don''t belong.

There is no C macro ''foo'' such that ''foo((bar))'' replaces to ''bar''
in general for all text ''bar'', if that''s what you''re looking for.
What are you *really* trying to do, and why?

-Arthur




" Karim Thapa" < KA ******** @ yahoo.com>在留言中写道

news:cd ************************** @ posting.google.c om ...

"Karim Thapa" <ka********@yahoo.com> wrote in message
news:cd**************************@posting.google.c om...
为什么下面的宏不起作用?

#define removebrace(x)x

void Foo(int a,int b,char * txt ,int d,int e);

main()
{

..
Foo(1,2,removebrace((hello ;,5,6)));


这相当于:


Foo(1,2,(hello,5,6));


反过来相当于:


Foo(1,2,6);


自从Foo的签名表示期待5个参数,这一个

将产生错误。了解/阅读逗号运算符。


[..]
任何帮助,为什么MACRO removebrace不起作用。我使用的是MSVC 6.
Why following macro does not work?

#define removebrace(x) x

void Foo(int a, int b, char *txt, int d, int e);

main()
{
..
Foo(1, 2, removebrace(("hello", 5, 6)) );
This is equivalent to:

Foo ( 1,2, ("hello", 5, 6 ) );

which in turn is equivalent to:

Foo (1, 2, 6 );

Since the signature of Foo says expect 5 arguments, this one
will generate an error. Know/read about comma operator.

[..]
Any help, why MACRO removebrace does not work. I use MSVC 6.




这与MSVC 6没有任何关系。


你的问题解决了你更换


Foo(1,2,removebrace((hello,5,6)));


with the br />

Foo(1,2,removebrace(" hello",5,6));


[..]


-

Vijay Kumar R Zanvar

我的主页 - http://www.geocities.com/vijoeyz/


文章< bu ** ********* *@ID-203837.news.uni-berlin.de> ;,

" Vijay Kumar R Zanvar" < 6 ***** @ hotpop.com>写道:
In article <bu************@ID-203837.news.uni-berlin.de>,
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote:
" Karim Thapa" < KA ******** @ yahoo.com>在消息中写道
新闻:cd ************************** @ posting.google.c om ...
"Karim Thapa" <ka********@yahoo.com> wrote in message
news:cd**************************@posting.google.c om...
为什么下面的宏不起作用?

#define removebrace(x)x

void Foo(int a,int b,char * txt,int d, int e);

main()
$

..
Foo(1,2,removebrace((hello,5, 6)));
Why following macro does not work?

#define removebrace(x) x

void Foo(int a, int b, char *txt, int d, int e);

main()
{
..
Foo(1, 2, removebrace(("hello", 5, 6)) );



这相当于:

Foo(1,2,(hello,5,6));

反过来相当于:

Foo(1,2,6);

因为Foo的签名表示期待5个参数,这个
会产生错误。了解/阅读逗号运算符。

[...]



This is equivalent to:

Foo ( 1,2, ("hello", 5, 6 ) );

which in turn is equivalent to:

Foo (1, 2, 6 );

Since the signature of Foo says expect 5 arguments, this one
will generate an error. Know/read about comma operator.

[..]


任何帮助,为什么MACRO removebrace不起作用。我使用MSVC 6.

Any help, why MACRO removebrace does not work. I use MSVC 6.



这与MSVC 6没有任何关系。

如果你更换你的问题就解决了

Foo(1,2,removebrace((hello,5,6)));

Foo(1,2,removebrace(")你好,5,6(6));



This hasn''t got anyting to do with MSVC 6.

Your problem solves if you replace

Foo(1, 2, removebrace(("hello", 5, 6)) );

with

Foo(1, 2, removebrace("hello", 5, 6) );




很可能不是,因为现在你将三个参数传递给一个只需要预期的宏

一个。



Most likely not, because now you are passing three arguments to a macro
that only expects one.


这篇关于奇怪的MACRO问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆