它是未定义的行为#define /#undef一个具有特殊意义的标识符? [英] Is it undefined behavior to #define/#undef an identifier with special meaning?

查看:389
本文介绍了它是未定义的行为#define /#undef一个具有特殊意义的标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

禁用在gcc中覆盖的检查的问题的答案建议使用 -Doverride = ,以禁用错误使用覆盖的错误>,它实际上与添加相同:

  #define override 
pre>

到源文件。



我的初步反应是,这看起来像未定义的行为,因为我们重新定义关键字,但是查看草稿C ++ 11标准部分 2.12 关键字[lex.key] 我惊讶的是, / em>或 final 都是关键字。它们在上一节 2.11 [lex.name] 中有说明,它们是具有特殊含义的标识符:


...


表3中的标识符在出现在
时有特殊的含义[...]


表3 标记为具有特殊含义的标识符,包括覆盖和<



问题是,它是未定义的行为,重新定义(使用#define / em>?

解决方案

如果您使用的是C ++标准库是未定义的行为以重新定义具有特殊含义的标识符,这也适用于关键字。根据 17.6.4 [constraints] 下的C ++ 11标准草案,我们有 17.6.4.1 [constraints.overview] 这说明:


使用
设施的C ++标准库[...]


17.6。 4 我们有 17.6.4.3.1 [macro.names] 部分:


翻译单元不应将#define或#undef命名为
与关键字相同,对于表3中列出的标识符或$ b $表3列出了具有特殊含义的标识符 / em>。我们可以看到此段落还包括关键字,并以相同的方式对待。


An answer to the question Disable check for override in gcc suggested using -Doverride= on the command line to disable errors for erroneous use of override, which is effectively the same as adding:

#define override

to the source file.

My initial reaction was that this seems like undefined behavior since we are redefining a keyword but looking at the draft C++11 standard section 2.12 Keywords [lex.key] I was surprised that neither override nor final are keywords. They are covered in the previous section 2.11 [lex.name] which says they are identifiers with special meaning:

The identifiers in Table 3 have a special meaning when appearing in a certain context[...]

and Table 3 is labelled Identifiers with special meaning and includes both override and final.

The question is, is it undefined behavior to redefine(using #define) identifiers with special meaning? Are they treated any differently than keywords in this respect?

解决方案

If you are using the C++ standard library it is undefined behavior to redefine identifiers with special meaning, this also applies to keywords. From the draft C++11 standard under section 17.6.4 [constraints] we have section 17.6.4.1 [constraints.overview] which says:

This section describes restrictions on C++ programs that use the facilities of the C++ standard library [...]

and under 17.6.4 we have section 17.6.4.3.1 [macro.names] which says:

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.

Table 3 list the Identifiers with special meaning. We can see this paragraph also covers keywords and they are treated in the same manner.

这篇关于它是未定义的行为#define /#undef一个具有特殊意义的标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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