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

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

发布时间:2020-12-16 05:21:32 所属栏目:百科 来源:网络整理
导读:例: 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 ma
例:
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提供一个(尽管它是相当神秘的).所有三个编译器的行为都是有效的.

(编辑:李大同)

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

    推荐文章
      热点阅读