C++11 中提议的无限制联盟是什么?

发布于 2024-10-30 04:56:20 字数 66 浏览 0 评论 0原文

我将无限制联合视为 C++11 中提出的功能之一。谁能解释一下这背后的语义及其提供的优势吗?

I gather unrestricted unions as one of the functionality being put forth in C++11. Can anyone please explain the semantics behind this and the advantages it provides?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花期渐远 2024-11-06 04:56:20

维基百科上有一个解释: http://en.wikipedia.org/wiki/C%2B%2B0x #Unrestricted_unions

在询问 C++0x 功能解释之前先搜索那里。

不受限制的工会

在标准 C++ 中
对什么类型有限制
对象可以是联合的成员。
例如,联合不能包含任何
定义非平凡的对象
构造函数。 C++0x 会减轻一些
这些限制允许工会
用于更多类型
以前不允许使用
上。[6]这是一个简单的例子
C++0x 中允许使用联合:

//用于放置新内容
#include <新>

结构点{
    观点() {}
    点(int x,int y):x_(x),y​​_(y){}
    整数x_,y_;
};
并集 U {
    整数 z;
    双w;
    点p; // 在 C++ 中非法; point 有一个不平凡的构造函数。 
              // 然而,这在 C++0x 中是合法的。
    U() { new( &p ) 点(); } // 没有重要的成员函数
                               //为联合隐式定义;
                               // 如果需要,它们会被删除
                               // 强制手动定义。
};

这些更改不会破坏任何
现有代码,因为它们只是放松
当前规则。

There is an explaination on Wikipedia : http://en.wikipedia.org/wiki/C%2B%2B0x#Unrestricted_unions

Search there first before asking about C++0x features explainations.

Unrestricted unions

In Standard C++
there are restrictions on what types
of objects can be members of a union.
For example, unions cannot contain any
objects that define a non-trivial
constructor. C++0x will alleviate some
of these restrictions, allowing unions
to be used on more types that they
were previously not allowed to be used
on.[6] This is a simple example of a
union permitted in C++0x:

//for placement new
#include <new>

struct Point  {
    Point() {}
    Point(int x, int y): x_(x), y_(y) {}
    int x_, y_;
};
union U {
    int z;
    double w;
    Point p;  // Illegal in C++; point has a non-trivial constructor. 
              //   However, this is legal in C++0x.
    U() { new( &p ) Point(); } // No nontrivial member functions are
                               //implicitly defined for a union;
                               // if required they are instead deleted
                               // to force a manual definition.
};

The changes will not break any
existing code since they only relax
current rules.

朱染 2024-11-06 04:56:20

它只不过是我们一直拥有的旧联合,一个一次包含一个不同类型成员的对象。

变化只是您现在可以在联合中存储非 POD 类型。但是,您将负责显式构造和销毁该成员。

来自 N3242:

[ 示例:考虑具有非静态数据的联合类型 U 的对象 u
M 类型的成员 m 和 N 类型的成员 n。如果 M 有一个非平凡的析构函数并且 N 有一个非平凡的构造函数
(例如,如果它们声明或继承虚拟函数),u的活动成员可以安全地切换
使用析构函数和放置新运算符从 m 到 n 如下:
嗯~M();
新 (&un) N;
——示例结束]

在我看来,这不是一个广泛有用的功能。

It is nothing else than the old unions we have always had, an object containing one member at a time, of varying type.

The change is just that you are now allowed to store non-POD types in a union. However, you will then be responsible for explicitly constructing and destroying that member.

From N3242:

[ Example: Consider an object u of a union type U having non-static data
members m of type M and n of type N. If M has a non-trivial destructor and N has a non-trivial constructor
(for instance, if they declare or inherit virtual functions), the active member of u can be safely switched
from m to n using the destructor and placement new operator as follows:
u.m.~M();
new (&u.n) N;
—end example ]

Not a widely useful feature, IMO.

仲春光 2024-11-06 04:56:20

它扩展联合以允许任何类型,而不仅仅是“普通旧数据”,使您能够更灵活地在同一位置存储不同类型的数据,而无需诉诸手动指针黑客技术。

为此付出的代价是你必须仔细记账。使用普通的旧数据联合分配足以更改“当前类型”,并且读取错误的类型可能会导致乱码数据,但仅此而已。对于非普通旧数据联合,您必须跟踪当前类型并手动调用正确的构造函数和析构函数来更改当前类型,并在销毁整个联合时正确清理内容。如果您尝试读取或写入 wring 类型,则可能会发生不好的事情

It expands unions to allow any type, not just "plain old data", giving you more flexibility to store different types of data in the same location without resorting to manual pointer hackery.

The price you pay for this is that you have to do some careful book keeping. With a plain old data union assignment was enough to change the "current type" and reading the wrong type was likely to result in garbled data but not much more than that. With a non plain old data union you must keep track of the current type and call the correct constructors and destructors manually to change the current type and to clean things up correctly when destroying the union as a whole. If you try to read or write the wring type bad things are likely to happen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文