问题描述
您将如何在标准C ++ 11/14中执行此操作?因为如果我没记错的话,这不是具有匿名结构的标准兼容代码。
How would you go about doing this in standard C++11/14 ? Because if I'm not mistaken this isn't standard compliant code with the anonymous structs.
我希望以与您相同的方式访问成员。 / p>
I wish to access the members the same way as you would with this.
template <typename some_type>
struct vec
{
union {
struct { some_type x, y, z; };
struct { some_type r, g, b; };
some_type elements[3];
};
};
推荐答案
是的,C ++ 11和C ++都不14允许匿名结构。 包含这种情况的一些原因。您需要命名结构,它们也不能在匿名联合中定义。
Yes, neither C++11 nor C++14 allow anonymous structs. This answer contains some reasoning why this is the case. You need to name the structs, and they also cannot be defined within the anonymous union.
§9.5/ 5 [class.union]
因此将结构定义移到并集之外。
So move the struct definitions outside of the union.
template <typename some_type>
struct vec
{
struct xyz { some_type x, y, z; };
struct rgb { some_type r, g, b; };
union {
xyz a;
rgb b;
some_type elements[3];
};
};
现在,我们要求 some_type
为 standard-layout ,因为这使匿名联合的所有成员 layout兼容。 。在标准的第§9/ 7节中对此进行了描述。
Now, we require some_type
to be standard-layout because that makes all the members of the anonymous union layout compatible. Here are the requirements for a standard layout type. These are described in section §9/7 of the standard.
然后,来自§9.2[class.mem]
对于数组成员,来自§3.9/ 9 [basic.types]
要确保 some_type
是标准布局,请在 vec
To ensure that some_type
is standard layout, add the following within the definition of vec
static_assert(std::is_standard_layout<some_type>::value, "not standard layout");
在 type_traits
标头中定义。现在,联合的所有3个成员都是标准布局,两个结构和数组都与布局兼容,因此这3个联合成员共享一个公共的初始序列,这使您可以编写然后检查(读取)属于该公共的任何成员初始序列(整个情况)。
std::is_standard_layout
is defined in the type_traits
header. Now all 3 members of your union are standard layout, the two structs and the array are layout compatible, and so the 3 union members share a common initial sequence, which allows you to write and then inspect (read) any members belonging to the common initial sequence (the entire thing in your case).
这篇关于匿名联合和结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!