C ++ 11与非平凡成员的匿名联合 [英] C++11 anonymous union with non-trivial members
问题描述
我正在更新我的结构,我想添加一个std :: string成员。原始结构如下所示:
struct Value {
uint64_t lastUpdated;
union {
uint64_t ui;
int64_t i;
float f;
bool b;
};
};
只要向union添加std :: string成员,当然会导致编译错误,因为通常需要添加对象的非平凡构造函数。 如果是std :: string(来自informit.com的文字)
由于std :: string定义了所有六个特殊成员函数,U将有一个隐式删除的默认构造函数,拷贝构造函数, ,移动构造函数,移动赋值运算符和析构函数。实际上,这意味着您不能创建U的实例,除非您明确定义一些或所有的特殊成员函数。
然后网站继续给出以下示例代码:
union U
{
int a;
int b;
string s;
U();
〜U();
};
但是,我在结构中使用匿名联合。我问了freenode上的## C ++,他们告诉我正确的方法是把构造函数放在结构中,并给我这个示例代码:
#include< new>
struct Point {
Point(){}
Point(int x,int y):x_(x),y_(y){}
int x_ ,y_;
};
struct Foo
{
Foo(){new(& p)Point(); }
union {
int z;
double w;
Point p;
};
};
int main(void)
{
}
但是从那里我无法想象如何使std :: string需要定义的特殊函数的其余部分,而且,我不完全清楚该例子中的ctor是如何工作的。
我可以让别人更清楚地解释这个问题吗?
变量成员不会被编译器生成的构造函数初始化,但应该没有麻烦选择一个并使用正常 ctor-initializer-list 。在匿名联合体中声明的成员实际上是包含类的成员,可以在包含类的构造函数中初始化。
这一行为在第9.5节中描述。 [class.union]
:
em> class是一个联合或一个具有作为直接成员的匿名联合的类。类似于union的类
X
有一组变量成员。如果X
是联合,其变体成员是非静态数据成员;否则,其变体成员是属于X
的所有匿名联合的非静态数据成员。
和第12.6.2节 [class.base.init]
:
一个 ctor-initializer 可以初始化构造函数类的变量成员。如果 ctor-initializer 为同一个成员或同一个基类指定了多个 mem-initializer ,则 ctor-initializer
所以代码可以简单:
#include< new>
struct Point {
Point(){}
Point(int x,int y):x_(x),y_(y){}
int x_ ,y_;
};
struct Foo
{
Foo():p(){} //在ctor初始化器中通常的日常初始化
union {
int z ;
double w;
Point p;
};
};
int main(void)
{
}
当然,当生成一个变量成员而不是在构造函数中初始化的其他成员时,仍应使用placement new。
I'm updating a struct of mine and I was wanting to add a std::string member to it. The original struct looks like this:
struct Value {
uint64_t lastUpdated;
union {
uint64_t ui;
int64_t i;
float f;
bool b;
};
};
Just adding a std::string member to the union, of course, causes a compile error, because one would normally need to add the non-trivial constructors of the object. In the case of std::string (text from informit.com)
Since std::string defines all of the six special member functions, U will have an implicitly deleted default constructor, copy constructor, copy assignment operator, move constructor, move assignment operator and destructor. Effectively, this means that you can't create instances of U unless you define some, or all of the special member functions explicitly.
Then the website goes on to give the following sample code:
union U
{
int a;
int b;
string s;
U();
~U();
};
However, I'm using an anonymous union within a struct. I asked ##C++ on freenode and they told me the correct way to do that was to put the constructor in the struct instead and gave me this example code:
#include <new>
struct Point {
Point() {}
Point(int x, int y): x_(x), y_(y) {}
int x_, y_;
};
struct Foo
{
Foo() { new(&p) Point(); }
union {
int z;
double w;
Point p;
};
};
int main(void)
{
}
But from there I can't figure how to make the rest of the special functions that std::string needs defined, and moreover, I'm not entirely clear on how the ctor in that example is working.
Can I get someone to explain this to me a bit clearer?
There is no need for placement new here.
Variant members won't be initialized by the compiler-generated constructor, but there should be no trouble picking one and initializing it using the normal ctor-initializer-list. Members declared inside anonymous unions are actually members of the containing class, and can be initialized in the containing class's constructor.
This behavior is described in section 9.5. [class.union]
:
A union-like class is a union or a class that has an anonymous union as a direct member. A union-like class
X
has a set of variant members. IfX
is a union its variant members are the non-static data members; otherwise, its variant members are the non-static data members of all anonymous unions that are members ofX
.
and in section 12.6.2 [class.base.init]
:
A ctor-initializer may initialize a variant member of the constructor’s class. If a ctor-initializer specifies more than one mem-initializer for the same member or for the same base class, the ctor-initializer is ill-formed.
So the code can be simply:
#include <new>
struct Point {
Point() {}
Point(int x, int y): x_(x), y_(y) {}
int x_, y_;
};
struct Foo
{
Foo() : p() {} // usual everyday initialization in the ctor-initializer
union {
int z;
double w;
Point p;
};
};
int main(void)
{
}
Of course, placement new should still be used when vivifying a variant member other than the other initialized in the constructor.
这篇关于C ++ 11与非平凡成员的匿名联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!