标准中的哪个地方说编译器应该忽略U :: j的默认成员初始值设定项?

Ayr*_*osa 10 c++ unions language-lawyer c++17

请考虑以下代码段:

#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';
}
Run Code Online (Sandbox Code Playgroud)

代码打印(参见实例):

1
1
Run Code Online (Sandbox Code Playgroud)

标准中的哪个部分表示U::j编译器忽略了该成员的默认成员初始值设定项?

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

查看实例:

union U{
    int i = 1;
    int j = 2;
};
Run Code Online (Sandbox Code Playgroud)

Jon*_*ely 7

标准中的哪个地方说编译器忽略了成员U :: j的默认成员初始值设定项?

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

注意你的演示有不确定的行为,因为联盟的活跃成员是i你读的j.这适用于某些编译器作为非标准扩展,但在ISO C++中是不允许的.