关键词重新定义在C / C ++ [英] Keywords redefinition in C / C++

查看:189
本文介绍了关键词重新定义在C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以重新定义的#define关键字用C?

Can i redefine keywords with #define in C?

我发现这个在C ++标准:

I found this in C++ standards:

ISO / IEC 14882:1998和ISO / IEC 14882:2003

17.4.3.1.1宏名称[lib.macro.names]

17.4.3.1.1 Macro names [lib.macro.names]

2转换单元包括标题,不得含有任何
  定义或声明该标题定义名称的宏。也不得
  这样的翻译单元定义宏的名称词汇相同
  关键字。

2 A translation unit that includes a header shall not contain any macros that define names declared or defined in that header. Nor shall such a translation unit define macros for names lexically identical to keywords.

164),它是不允许被删除库宏定义
  使用#undef指令。

164) It is not permissible to remove a library macro definition by using the #undef directive.

ISO / IEC 14882:2011

17.6.4.3.1宏名称[macro.names]

17.6.4.3.1 Macro names [macro.names]

2译文单位不得#定义或词汇和#undef名
  相同的关键字,以在表3中列出的标识符,或往
  7.6所述属性令牌。

2 A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table 3, or to the attribute-tokens described in 7.6.

所以,我们不能重新定义的关键字C ++ 98 / C ++ 03,如果我们包括标准C ++库的头文件,而在C ++ 11,我们不能做任何翻译单元,右?

So, we can't redefine keywords in C++98 / C++03 if we include any header files from Standard C++ Library, while in C++11 we can't do it in any translation unit, right?

推荐答案

你能吗?是的,大多数编译器不会强行prevent它。

Can you? Yes, most compilers don't forcibly prevent it.

你是否应该?不,(几乎)从来没有,这是(通常)一个可怕的想法。各种各样的事情可能会破裂,并与大多数preprocessor废话,这将是很难调试。

Should you? No, (almost) never, it's (usually) a horrible idea. All sorts of things may break, and as with most preprocessor nonsense, it's going to be very difficult to debug.

因此​​,可以合法的?没有。

Therefore, can you legally? No.

在相同的方式,你的可以的有与启动__ 或添加的东西到 STD宏或变量:: 命名空间或其他各种非常坏事。然而,像这样的大多数规则有很好的理由(与关键字或名称解析冲突,主要是)。虽然它可能是身体可能,很少有(如果有的话),有时你的需要的少还是当你的

In the same fashion, you can have macros or variables that start with __ or add things to the std:: namespace or all sorts of other Very Bad Things. However, most rules like this have Very Good Reasons (conflicts related to keywords or name resolution, mostly). While it may be physically possible, there are few (if any) times you need to and fewer still when you should.

这篇关于关键词重新定义在C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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