samedi 20 août 2016

Initialize more than one non static data member of union

Consider following program which is ill formed according to standard

union Test {
    int s{3};
    float f;
    Test() {}            
    Test(float s) : f(s) {} // this should be error
  };
int main() {

}

C++11 standard N3376 clause 12 section 6.2.8 says that ( emphasis mine):

An attempt to initialize more than one non-static data member of a union renders the program ill-formed.

But all the popular 3 compilers ( g++, clang++, MSVC++ ) compiles above program without producing any compiler error or warning. I think it is necessary for a compiler to give diagnosis in this program & program should fail in compilation.

See live demo tested on g++ here.

See live demo tested on clang++ here.

Are all the compilers broken here according to standard ? Is this compiler bug ?

Aucun commentaire:

Enregistrer un commentaire