即使我使用-std=c++11
标志进行编译,GCC也提示此代码,并且我的gcc版本据说支持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];
};
};
请注意,float4具有一个非默认构造函数,该构造函数接受0参数。
class float4
{
public:
float4();
....
};
对我能做什么有任何想法吗?错误是:
<anonymous union>::<anonymous struct>::I’ with constructor not allowed in anonymous aggregate
最佳答案
这里的问题不是事实,您的类float4
具有构造函数,使其在POD的旧C++ 03定义下成为非POD。相反,问题在于您的 union 和 union 中的结构是匿名的。如果您简单地命名它们,它将起作用:
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];
};
};
关于c++ - 具有构造函数的类的匿名 union/结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9654751/