加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – typedef并在相同范围内使用同名的声明

发布时间:2020-12-16 06:02:19 所属栏目:百科 来源:网络整理
导读:我研究了C11标准(好吧,n3242草案)和互联网,但找不到确切的答案.下面的代码可以用clang 3.2和g 4.7.2以及Visual Studio 2010进行编译,但是我希望得到一个错误. #include iostream#include typeinfotypedef int a_t;namespace a_ns{class a_t {};}using a_ns::
我研究了C11标准(好吧,n3242草案)和互联网,但找不到确切的答案.下面的代码可以用clang 3.2和g 4.7.2以及Visual Studio 2010进行编译,但是我希望得到一个错误.
#include <iostream>
#include <typeinfo>


typedef int a_t;


namespace a_ns
{
class a_t {};
}


using a_ns::a_t;


int main()
{
    a_t a;
    std::cout << typeid(a).name() << std::endl;
    return 0;
}

建于:

clang -std=c++11 -pedantic -Wall -o a a.cpp -lstdc++
g++ -std=c++11 -pedantic -Wall -o a a.cpp -lstdc++
cl -EHsc -GR a.cpp

clang和g生成的可执行文件打印“i”,这似乎表明a是int类型,typedef占上风. cl生成可执行文件“class a_ns :: a_t”,这似乎表明Visual Studio更喜欢使用声明.

我希望代码不能按照以下标准摘录编译.我会期待一个类似于“已经在范围内使用声明冲突的声明”的错误.

7.1.3.6 Similarly,in a given scope,a class or enumeration shall not be declared with the same name as a typedef-name that is declared in
that scope and refers to a type other than the class or enumeration
itself.

7.3.3.1 A using-declaration introduces a name into the declarative region in which the using-declaration appears.

7.3.3.2 Every using-declaration is a declaration […]

可能有一些我在标准中缺少解释这个行为的东西(或者我太累了,看不清楚),但我似乎找不到.

谢谢.

解决方法

没错,你所显示的代码无效.还有3.3.1p4也使它无效(见7.3.3p13).

对于现实测试,我用ICC测试,并按预期方式拒绝.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读