微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 成员声明`decltype(name)name;`在局部结构中允许的名字是指封闭的范围?

例:
int main()
{
    int a = 0;
    struct X
    {
        decltype(a) a;
    };
    return 0;
}

decltype(a)是指主要的本地a,而它声明的成员共享相同的名称.

Clang编译没有任何问题,MSVC14也是如此.

G++抱怨说,添加 – 无限制使它通过

prog.cc:6:21: error: declaration of 'int main()::X::a' [-fpermissive]
         decltype(a) a;
                     ^
prog.cc:3:9: error: changes meaning of 'a' from 'int a' [-fpermissive]
     int a = 0;

哪种行为符合标准?

解决方法

我相信这违反了[basic.scope.class] / 1(N3337):

The following rules describe the scope of names declared in classes.

1) […]

2) A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

由于decltype(a)是指在声明成员变量之前的封闭范围内的声明,而是在“X的完整范围内重新评估”时引用该成员,该程序是不正确的.不需要诊断,但GCC提供一个(尽管它是相当神秘的).所有三个编译器的行为都是有效的.

原文地址:https://www.jb51.cc/c/114909.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐