简体   繁体   中英

Anonymous union/struct of class with constructor

GCC complains about this code even though I compile with -std=c++11 flag, and my gcc version supposedly supports Unrestricted unions (>4.6).

union 
{
    struct 
    {
        float4 I,J,K,T;
    };
    struct 
    {
        float4 m_lines[4];
    };
    struct
    {
        float m16f[16];
    };
    struct 
    {
        float m44f[4][4];
    };
};

Note that float4 has a non-default constructor that takes 0 parameters.

class float4 
{   
    public:
       float4();
 ....
};

Any idea of what I could do ? The error is :

<anonymous union>::<anonymous struct>::I’ with constructor not allowed in anonymous aggregate

The issue here is not the fact that your class float4 has a constructor, making it a non-POD under the old C++03 definition of POD. Rather, the issue is that your union and the structs within your union are anonymous. If you simply name them, it will work:

union u
{
    struct s1
    {
        float4 I,J,K,T;
    };
    struct s2
    {
        float4 m_lines[4];
    };
    struct s3
    {
        float m16f[16];
    };
    struct s4
    {
        float m44f[4][4];
    };
};
union // This do not need named, it will same to MSVC10 or XCode
{
    struct s1
    {
        float4 I,J,K,T;
    };
    struct s2
    {
        float4 m_lines[4];
    };
    struct s3
    {
        float m16f[16];
    };
    struct s4
    {
        float m44f[4][4];
    };
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM