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

c – 标准中的哪个地方说,U :: j的默认成员初始化程序应该被编译

发布时间:2020-12-16 06:04:34 所属栏目:百科 来源:网络整理
导读:请考虑以下代码段: #include iostreamunion U{ U(): i(1) {} int i; int j = 2; // this default member initializer is ignored by the compiler};U u;int main(){ std::cout u.i 'n'; std::cout u.j 'n';} 代码打印(见live example): 11 标准中的哪个
请考虑以下代码段:
#include <iostream>
union U{
    U(): i(1) {}
    int i;
    int j = 2;    // this default member initializer is ignored by the compiler
};

U u;

int main(){
    std::cout << u.i << 'n';
    std::cout << u.j << 'n';
}

代码打印(见live example):

1
1

标准中的哪个地方说,编译器忽略成员U :: j的默认成员初始化器?

请注意,下面的联合不编译,这可以根据[class.union.anon]/4.我期待上面的代码段也不编译.

见live example:

union U{
    int i = 1;
    int j = 2;
};

解决方法

Where in the Standard does it say that the default member initializer for the member U::j is ignored by the compiler?

请参阅C 17 CD中的[class.base.init]第9段子弹9.1.

注:您的演示具有未定义的行为,因为联盟的活动成员是我,但是您从j读取.这适用于一些编译器作为非标准扩展,但ISO C中不允许.

(编辑:李大同)

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

    推荐文章
      热点阅读