命名空间和#define [英] namespace and #define

查看:113
本文介绍了命名空间和#define的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我注意到当我在命名空间中使用#define时,这个#define只有

在命名空间名称前缀时才会起作用。 />

这种行为是标准的还是只是编译器特定的?


我使用VC7。


命名空间测试< br $>
{

#define X int

};


现在可以用作:


测试:: X


-

Elias

解决方案



" lallous" < LA ***** @ lgwm.org>在消息中写道

Hello

我注意到当我在命名空间中使用#define时,这个#define
仅在命名空间名称前缀时才有效。这个行为是标准的还是只是编译器特定的?

我使用VC7。

命名空间测试
{
#define X int
};


命名空间定义后没有分号。

现在可以用作:

test :: X



IMO,这是不正确的行为。宏在没有采取

帐户命名空间规则等的情况下进行替换。因此宏应该将test :: X扩展为test :: int

这是非语义的。只是使用X应该是好的。


Sharad


所以宏应该将test :: X扩展为test :: int




用预处理器替换宏。


" lallous" ; < LA ***** @ lgwm.org>在留言中写道

news:2u ************* @ uni-berlin.de ...

我注意到我用的时候在命名空间内#define然后这个#define
只有在命名空间名称前缀时才有效。

这种行为是标准的还是只是编译器特定的?
绝对不标准。

我认为你误解了发生了什么。

我使用VC7。

命名空间测试
{
#define X int
};

现在可以用作:

test :: X




没有编译器应该接受这个,而且我不熟悉

在VC7或其他地方有任何此类扩展。

你能否准确发布导致你得出结论的代码?


问候,

伊万

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++<> http://www.brainbench.com

Hello

I noticed that when I use #define inside a namespace then this #define only
works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific?

I use VC7.

namespace test
{
#define X int
};

Now can be used as:

test::X

--
Elias

解决方案


"lallous" <la*****@lgwm.org> wrote in message

Hello

I noticed that when I use #define inside a namespace then this #define only works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific?

I use VC7.

namespace test
{
#define X int
};
There is no semicolon after a namespace definition.

Now can be used as:

test::X



IMO, this is not correct behavior. Macros do replacement without taking
account namespace rules etc. So macro should expand test::X to test::int
which is non-sensical. Just using X should have been good.

Sharad


So macro should expand test::X to test::int



Replace macro with preprocessor.


"lallous" <la*****@lgwm.org> wrote in message
news:2u*************@uni-berlin.de...

I noticed that when I use #define inside a namespace then this #define
only works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific? Definitely not standard.
And I think you misinterpreted what happened.
I use VC7.

namespace test
{
#define X int
};

Now can be used as:

test::X



No compiler should accept this, and I''m not familiar
with any such extension in VC7 or elsewhere.
Could you post the exact code that lead you to your conclusion?

Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


这篇关于命名空间和#define的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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