在这个矩阵类中使用 union 完全安全吗?
联合并不是我经常使用的东西,在查看了有关它们的其他一些问题之后,似乎总是存在某种它们可能不起作用的警告。例如。结构可能具有意外的填充或字节序差异。
在我正在使用的数学库中遇到了这个,我想知道它是否是一个完全安全的用法。我假设多维数组没有任何额外的填充,并且由于两个定义的类型相同,因此它们保证占用完全相同的内存量?
template<typename T> class Matrix44T
{
...
union
{
T M[16];
T m[4][4];
} m;
};
这种设置有什么缺点吗?定义的顺序会对它的工作方式产生任何影响吗?
Unions aren't something I've used that often and after looking at a few other questions on them here it seems like there is almost always some kind of caveat where they might not work. Eg. structs possibly having unexpected padding or endian differences.
Came across this in a math library I'm using though and I wondered if it is a totally safe usage. I assume that multidimensional arrays don't have any extra padding and since the type is the same for both definitions they are guaranteed to take up exactly the same amount of memory?
template<typename T> class Matrix44T
{
...
union
{
T M[16];
T m[4][4];
} m;
};
Are there any downsides to this setup? Would the order of definition make any difference to how this works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然我在 Matrix 类中做了完全相同的事情,但我认为这是依赖于实现的,请逐字阅读标准:
标准 9.5.1:
接下来的问题是
m
和M
是否共享一个共同的初始序列,为了回答这个问题,我们看一下 9.2/15:读完这篇文章后,答案似乎是,从严格意义上来说,没有
m
和M
不兼容布局。实际上,我认为这在所有编译器上都可以正常工作。
Although I do exactly the same in my Matrix-class I think this is implementation dependent, reading the standard to the letter:
Standard 9.5.1:
The question then is do
m
andM
share a common initial sequence, to answer this we look at 9.2/15:After reading this the answer seems to be, no
m
andM
are not layout-compatible in the strict sense of the word.In practice I think this will work fine on all compilers though.
如果您遵守规则,填充和字节序差异不会对您造成伤害。
看一下这段代码
这是错误的,因为如果您编写了成员“a”,那么除了“a”之外的任何读取都是未定义的。
总结一下:工会是否安全无法得知。不安全的不是定义,而是它的使用方式。
If you play by the rules, padding and endian differences won't hurt you.
Look at this code
This is wrong because if you have written member "a", then any reading except from "a" is undefined.
To sum this up: Whether a union is safe cannot be told. It's not the definition, that is unsafe, it's how it is being used.
不。根据您的假设,这似乎是
union
的良好用法。我会选择更好的名称,而不是
m
和M
,但除此之外,它是union
的一个很好的用法。No. This seems just fine usage of
union
under your assumptions.I would have chosen better names and not
m
andM
but other than that it is a nice usage forunion
.