类声明在同一作用域中使用声明在GCC而不是MSVS中编译 [英] Class declaration in same scope as using declaration compiles in GCC but not MSVS

查看:135
本文介绍了类声明在同一作用域中使用声明在GCC而不是MSVS中编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序是否根据c ++标准格式良好?

 命名空间X {class A; } 

命名空间Y {using X :: A; class A {}; }

int main(){}

不同的编译器:




  • gcc编译时没有错误。

  • visual c ++给出错误C2888: X :: A':符号不能在命名空间'Y'中定义



我在c ++标准中找不到任何规则我的程序违反。



如果程序格式正确,为什么visual studio会出错?



如果程序没有很好的形式,在c ++标准中违反什么规则,为什么gcc不给错误?



我不是想我的程序编译。我只是想知道它是否根据c ++标准格式良好,为什么我测试的两个编译器的行为不同。

解决方案

我相信程序是不成形的。 [basic.scope.declarative] / 4说:


在单个声明区中给定一组声明,非限定名称,



- 它们都应指向同一个实体,或者全部指函数和函数模板;或



- 正好一个声明应声明一个不是typedef名称的类名或枚举名称,其他声明都应引用相同的变量或枚举值,参考功能和功能模板;在这种情况下类名或枚举名称是隐藏的


未限定名的两个声明 A 指的是不同的实体,两者都是类。



(有趣的是,GCC 6.0或者Clang 3.7似乎没有这种诊断方式。如果你把 X :: A a; 添加到 main的正文中,则Clang抱怨 X :: A 的不完整类型。)


Is the following program well-formed according to the c++ standard?

namespace X { class A; }

namespace Y { using X::A; class A {}; }

int main() {}

I'm getting different results with different compilers:

  • gcc compiles it without errors.
  • visual c++ gives error C2888: 'X::A': symbol cannot be defined within namespace 'Y'

I don't find any rule in the c++ standard that my program violates.

If the program is well-formed, why does visual studio give an error?

If the program is not well-formed, what rule in the c++ standard did it violate and why doesn't gcc give an error?

I'm not trying to make my program compile. I'm just trying to find out if it is well-formed according to the c++ standard and why the two compilers I tested behave differently.

解决方案

I believe the program is ill formed. [basic.scope.declarative]/4 says:

Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,

— they shall all refer to the same entity, or all refer to functions and function templates; or

— exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same variable or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden

The two declarations of unqualified name A refer to different entities, both of which are classes.

(Interestingly, neither GCC 6.0 nor Clang 3.7 seem to diagnose it that way. Both accept the code as written (not diagnosing the declaration of two distinct classes with the same name). If you add X::A a; to the body of main, then Clang complains about the incomplete type of X::A.)

这篇关于类声明在同一作用域中使用声明在GCC而不是MSVS中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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