这是有效的C ++代码吗? [英] Is this valid C++ code?

查看:126
本文介绍了这是有效的C ++代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,基本上是



  class foo {
public:
void method();
};

void foo :: foo :: method(){}



在foo :: method的定义前面意外添加了一个额外的foo ::。这段代码使用g ++(ver 4.2.3)编译没有警告,但使用Visual Studio 2005错误。我没有名为foo的命名空间。



哪个编译器是正确的?

解决方案

正确的,g ++是正确的,VS是错误的。



ISO-IEC 14882-2003(E),§9.2类(pag.153) :将类名插入到在看到类名后立即声明的范围中。类名也被插入到类本身的范围中;这被称为注入类名。为了访问检查的目的,inject-class-name被视为是一个公共成员名。



特别有用的是保留关于实际名称查找规则的以下内容:



ISO-IEC 14882-2003(E),§3.4-3Name Lookup 29)为了名称隐藏和查找的目的,类(第9节)的注入类名也被认为是该类的成员。

这是奇怪的,如果不是,给定的最后一部分的文本在9.2。但是litb评论说,这向我们保证,g ++确实正确地解释了标准。没有问题。


I had the following code, which was basically,

class foo {
  public:
    void method();
};

void foo::foo::method() { }

I had accidentally added an extra foo:: in front of the definition of foo::method. This code compiled without warning using g++(ver 4.2.3), but errored out using Visual Studio 2005. I didn't have a namespace named foo.

Which compiler is correct?

解决方案

If I read the standard correctly, g++ is right and VS is wrong.

ISO-IEC 14882-2003(E), §9.2 Classes (pag.153): A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

Following on the comments below, it's also particularly useful to retain the following concerning the actual Name Lookup rules:

ISO-IEC 14882-2003(E), §3.4-3 Name Lookup (pag.29): The injected-class-name of a class (clause 9) is also considered to be a member of that class for the purposes of name hiding and lookup.

It would be odd if it wasn't, given the final part of text at 9.2. But as litb commented this reassures us that indeed g++ is making a correct interpretation of the standard. No questions are left.

这篇关于这是有效的C ++代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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