Unions in C++11: default constructor seems to be deleted

后端 未结 2 996
孤独总比滥情好
孤独总比滥情好 2021-02-14 02:00

I am trying to understand how unions were extended by C++11. One thing that changed is the ability to use now non-static data members with non-trivial special member functions.

2条回答
  •  野的像风
    2021-02-14 02:40

    X is not a pod type because is not trivially copiable as it have destructor Also U is not a pod type.

    U s2; try to call the default costructor that is deleted so the error

    U s1 {}; use member wise initialization and don't call any costructor

    In union with non pod member the default costructor of union is deleted because it would call the default costructor of members ie the compiler doesn't know which member to call the default costructor

     Union XX{
       string m1; 
       vector  m2;
    }
    

    default costructor of XX cannot call default costructor of m1 AND m2 so it is deleted

提交回复
热议问题