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

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

概述请考虑以下代码段: #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'; 请考虑以下代码段:
#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):

11

标准中的哪个地方说,编译器忽略成员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段子d9.1.

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

总结

以上是内存溢出为你收集整理的c – 标准中的哪个地方说,U :: j的默认成员初始化程序应该被编译器忽略?全部内容,希望文章能够帮你解决c – 标准中的哪个地方说,U :: j的默认成员初始化程序应该被编译器忽略?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1252185.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存