...联合问题中不允许使用构造函数
我迫切需要找到以下问题的解决方案:
namespace test
{
template <int param = 0> struct Flags
{
int _flags;
Flags()
{
_flags = 0;
}
Flags(int flags)
{
_flags = flags;
}
void init()
{
}
};
union example
{
struct
{
union
{
struct
{
Flags<4096> f;
}p1; //error: member 'test::example::<anonymous struct>::<anonymous union>::<anonymous struct> test::example::<anonymous struct>::<anonymous union>::p1' with constructor not allowed in union
struct
{
Flags<16384> ff;
}p2; //error: member 'test::example::<anonymous struct>::<anonymous union>::<anonymous struct> test::example::<anonymous struct>::<anonymous union>::p2' with constructor not allowed in union
}parts;
byte bytes[8];
}data;
int data1;
int data2;
}
}
令人沮丧的是,如果我向 p1 和 p2 结构添加标签,代码将编译,但 f 和 p2 结构体将被编译。 ff 成员将不可访问:
...
struct p1
{
Flags<4096> f;
};
struct p2
{
Flags<4096> ff;
};
...
void test()
{
example ex;
ex.data.bytes[0] = 0; //Ok
ex.data.parts.p1.f.init(); //error: invalid use of 'struct test::example::<anonymous struct>::<anonymous union>::p1'
}
有什么办法可以让这项工作以某种方式进行吗?
I desperately need to find a solution for the following problem:
namespace test
{
template <int param = 0> struct Flags
{
int _flags;
Flags()
{
_flags = 0;
}
Flags(int flags)
{
_flags = flags;
}
void init()
{
}
};
union example
{
struct
{
union
{
struct
{
Flags<4096> f;
}p1; //error: member 'test::example::<anonymous struct>::<anonymous union>::<anonymous struct> test::example::<anonymous struct>::<anonymous union>::p1' with constructor not allowed in union
struct
{
Flags<16384> ff;
}p2; //error: member 'test::example::<anonymous struct>::<anonymous union>::<anonymous struct> test::example::<anonymous struct>::<anonymous union>::p2' with constructor not allowed in union
}parts;
byte bytes[8];
}data;
int data1;
int data2;
}
}
It's frustrating that if I add tags to p1 and p2 structs, the code will compile, but the f & ff members would not be accessible:
...
struct p1
{
Flags<4096> f;
};
struct p2
{
Flags<4096> ff;
};
...
void test()
{
example ex;
ex.data.bytes[0] = 0; //Ok
ex.data.parts.p1.f.init(); //error: invalid use of 'struct test::example::<anonymous struct>::<anonymous union>::p1'
}
Is there any way to make this work somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如@Als所说,union不能将非POD定义为成员数据,有一种替代方法。您仍然可以定义指向非 POD 的指针作为联合的成员数据。
所以这是允许的:
但是 Boost.Variant 是一个更好的选择。
As @Als said, union cannot define non-POD as member data, there is one alternative. You can still define a pointer to the non-POD as member data of the union.
So this is allowed:
But then Boost.Variant is a better alternative.
当前的 C++ 标准不允许联合内有非 POD 类型。因此,您会从 gcc 中收到此编译器错误。
您应该使用
boost::variant
,而不是使用 C 联合。请在此处查看文档。添加到上面:
新的 C++ 标准 (C++0x) 添加了一个名为 无限制的Unrestricted Unions,支持将非POD类型存储到Union中。
Current C++ standard does not allow non-POD types inside unions. Hence you get this compiler error from gcc.
Instead of using C unions, you should use
boost::variant
. Check the doccumentation here.To add to the above:
The new C++ standard(C++0x) adds a new feature called as Unrestricted Unions, which supports storing non-POD types to a Union.
C++ 标准 2003 不允许这样做(从标准 9.5 开始):
但是C++0x允许这样做,但是,您需要定义自己的构造函数,因为如果您不定义自己的构造函数,默认构造函数将被声明为删除。
从 N3291 9.5 开始:
其他人建议使用 Boost.Variant。对于简单的修复,只需从 Flag 类中删除构造函数即可。尽管每次初始化 union 对象时都需要手动初始化它,但这不会造成混淆,因为 union 本质上是 C 功能。
C++ standard 2003 doesn't allow this (from Standard 9.5):
But C++0x allows it, however, you need to define your own constructor because default constructor will be declared as deleted if you don't define your own.
From N3291 9.5:
Other people has suggested Boost.Variant. For simple fix, just remove the constructor from Flag class. Though you need to initialize it manually whenever you initialize the union object, it won't be confusing because union is essentially a C feature.