Skip to Main Content

Cannot initialize members of anonymous struct in a union

kovalAug 6 2017 — edited Aug 7 2017

The following code is accepted by clang and gcc. Studio 12.5 and 12.6 reject this

class A {

    union {

        struct {

            void* p1;

            void* p2;


        void* array[3];



    A(): p1(nullptr), p2(nullptr) { }



line 10: Error: Attempt to initialize multiple variant members in 'A' by initializing 'p2'.

Is this a case where Studio is better conforming to the standard or a bug?

This kind of code is used in Qt's QML engine but I have doubts about its correctness.

I am not sure what is the correct interpretation because when p1 and p2 are initialized inline (i.e. void* p1 = nullptr;) gcc gives error like multiple fields in union ‘A::<anonymous union>’ initialized

Post Details
Added on Aug 6 2017
1 comment