在C ++ 11中提出的无限制联合是什么? [英] What are Unrestricted Unions proposed in C++11?
问题描述
我收集无限制联盟
作为在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?
推荐答案
维基百科上有一个解释: http://en.wikipedia.org/wiki/C%2B%2B0x#Unrestricted_unions
There is an explaination on Wikipedia : http://en.wikipedia.org/wiki/C%2B%2B0x#Unrestricted_unions
先搜索询问C ++ 0x功能说明。
Search there first before asking about C++0x features explainations.
无限制联盟
在标准C ++
中,对于什么类型的
对象可以是联合的成员有限制。
例如,联合不能包含任何定义非平凡
构造函数的
对象。 C ++ 0x将减轻这些限制的一些
,允许工会
用于更多类型,因为他们
以前不允许使用
on。[6]这是C ++ 0x中允许的
联合的一个简单示例:
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.
这篇关于在C ++ 11中提出的无限制联合是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!