- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
重要的澄清:一些评论者似乎认为我是从 union 复制的。仔细查看 memcpy
,它从一个普通的旧 uint32_t
地址复制而来,该地址不包含在 union 中。另外,我正在(通过 memcpy
)复制到 union 的特定成员(u.a16
或 &u.x_in_a_union
,而不是直接复制到整个 union 本身 (&u
)
C++ 对 union 非常严格 - 只有当该成员是最后写入的成员时,您才应该从该成员中读取:
9.5 Unions [class.union] [[c++11]] In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time.
(当然,编译器不会跟踪哪个成员处于事件状态。由开发人员确保他们自己跟踪)
更新:以下代码块是主要问题,直接反射(reflect)问题标题中的文本。如果这段代码没问题,我会跟进其他类型,但我现在意识到这第一段代码本身很有趣。
#include <cstdint>
uint32_t x = 0x12345678;
union {
double whatever;
uint32_t x_in_a_union; // same type as x
} u;
u.whatever = 3.14;
u.x_in_a_union = x; // surely this is OK, despite involving the inactive member?
std::cout << u.x_in_a_union;
u.whatever = 3.14; // make the double 'active' again
memcpy(&u.x_in_a_union, &x); // same types, so should be OK?
std::cout << u.x_in_a_union; // OK here? What's the active member?
紧接在此上方的代码块可能是评论和答案中的主要问题。事后看来,我不需要在这个问题中混合类型!基本上,假设类型相同,u.a = b
是否与 memcpy(&u.a,&b, sizeof(b))
相同?
首先,一个相对简单的 memcpy
允许我们将 uint32_t
读取为 uint16_t
的数组:
#include <cstdint> # to ensure we have standard versions of these two types
uint32_t x = 0x12345678;
uint16_t a16[2];
static_assert(sizeof(x) == sizeof(a16), "");
std:: memcpy(a16, &x, sizeof(x));
确切的行为取决于您平台的字节序,您必须注意陷阱表示等。但这里普遍同意(我认为?感谢反馈!),注意避免有问题的值,上面的代码可以在正确的平台上的正确上下文中完全符合标准。
(如果您对上述代码有疑问,请相应地评论或编辑问题。我想确保在继续执行下面的“有趣”代码之前,我们有上述的无争议版本。 )
如果,且仅当,以上两段代码都不是-UB,那么我想将它们组合如下:
uint32_t x = 0x12345678;
union {
double whatever;
uint16_t a16[2];
} u;
u.whatever = 3.14; // sets the 'active' member
static_assert(sizeof(u.a16) == sizeof(x)); //any other checks I should do?
std:: memcpy(u.a16, &x, sizeof(x));
// what is the 'active member' of u now, after the memcpy?
cout << u.a16[0] << ' ' << u.a16[1] << endl; // i.e. is this OK?
union 的哪个成员 u.whatever
或 u.a16
是“活跃成员”?
最后,我自己的猜测是,我们在实践中关心这个的原因是优化编译器可能没有注意到 memcpy
发生了,因此做出错误的假设(但允许的假设,按照标准)关于哪个成员是事件的以及哪些数据类型是“事件的”,因此会导致别名错误。编译器可能会以奇怪的方式重新排序 memcpy
。 这是对我们为什么关心这个问题的恰当总结吗?
最佳答案
我对标准的解读是,只要类型可轻松复制,std::memcpy
都是安全的。
从 9 个类中,我们可以看到 union
是类类型,因此 trivially copyable 适用于它们。
A union is a class defined with the class-key union; it holds only one data member at a time (9.5).
A trivially copyable class is a class that:
- has no non-trivial copy constructors (12.8),
- has no non-trivial move constructors (12.8),
- has no non-trivial copy assignment operators (13.5.3, 12.8),
- has no non-trivial move assignment operators (13.5.3, 12.8), and
- has a trivial destructor (12.4).
trivially copyable 的确切含义在 3.9 类型中给出:
For any object (other than a base-class subobject) of trivially copyable type
T
, whether or not the object holds a valid value of typeT
, the underlying bytes (1.7) making up the object can be copied into an array ofchar
orunsigned char
. If the content of the array ofchar
orunsigned char
is copied back into the object, the object shall subsequently hold its original value.For any trivially copyable type
T
, if two pointers toT
point to distinctT
objectsobj1
andobj2
, where neitherobj1
norobj2
is a base-class subobject, if the underlying bytes (1.7) making upobj1
are copied intoobj2
,obj2
shall subsequently hold the same value asobj1
.
该标准还给出了两者的明确示例。
因此,如果您要复制整个 union ,答案肯定是肯定的,事件成员将与数据一起“复制”。 (这是相关的,因为它表明 std::memcpy
必须 被视为更改 union 的事件元素的有效方法,因为明确允许使用它 union 复制。)
现在,您改为复制到 union 的成员中。该标准似乎不需要任何特定的方法来分配给 union 成员(并因此使其处于事件状态)。它所做的只是指定(9.5)
[ Note: In general, one must use explicit destructor class and placement new operators to change the active member of a union. — end note]
当然,因为 C++11 允许在 union 中使用非平凡类型的对象。请注意前面的“一般”,它非常清楚地表明在特定情况下允许使用其他更改事件成员的方法;我们已经知道是这种情况,因为明确允许分配。当然,没有禁止使用 std::memcpy
,否则它的使用是有效的。
所以我的回答是肯定的,这是安全的,是的,它会改变事件成员。
关于c++ - memcpy/memmove 到 union 成员,这是否设置了 'active' 成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39763548/
自从我的问题here无法自信地回答,我在这里再次询问,希望有人确切知道: 指向 union 的指针和包含指向其元素的指针的 union 之间有什么区别(除了语法之外)吗? this中生成的程序集示例是
在 C 语言中,是否可以在另一个 union 体中定义一个 union 体?如果不是,为什么不可能?或者如果可以,可以在哪里使用? 最佳答案 假设您要定义: union myun { int x;
在 C 中,是否可以在另一个 union 中定义一个 union ?如果不是,为什么不可能?或者如果是,它可以在哪里使用? 最佳答案 假设你想定义: union myun { int x; s
我正在阅读一些代码并发现如下内容: typedef union { int int32; int boolean; time_t date; char *string;
我正在学习Lua,我更愿意使用冒号(:)作为方法。不幸的是,它并非在所有地方都有效。看我的代码: 设置= {} 本地mt = {} 函数Set:new(m) 本地集= {} setmetatable(
我遇到了一些性能问题,我有如下查询: SELECT * FROM Foo UNION SELECT * FROM Boo UNION SELECT * FROM Koo 我确信 Koo 不会返回任何重
This question already has answers here: C++ Structure Initialization (16个答案) 上个月关闭。 我正在尝试将一些用于嵌入式目标的
UNION 和 UNION ALL 有什么区别? 最佳答案 UNION 删除重复记录(结果中的所有列都相同),UNION ALL 则不会。 使用 UNION 而不是 UNION ALL 时会影响性能,
我想在两个表上使用联合运算符。我希望结果集消除由联合创建的重复值,但不消除表中预先存在的重复值。考虑这段代码... select b from (values (1), (2), (2
我知道 UNION 会删除重复项,但即使没有重复项也会更改结果顺序。 我有两个 select 语句,任何地方都没有 order by 语句 我想将它们合并或不合并(全部) 即 SELECT A UNI
基本上,我有一个 struct foo { /* variable denoting active member of union */ enum whichmembe
我有一个大规模查询,用于对许多表(每个表有数千行)执行 UNION ALL,然后在返回之前输出到临时表。 旧形式: SELECT * FROM (SELECT `a` AS `Human rea
UNION 和 UNION ALL 有什么区别? 最佳答案 UNION 删除重复记录(结果中的所有列都相同),UNION ALL 则不会。 使用 UNION 而不是 UNION ALL 时会影响性能,
如果我有两个 union 行结构: struct A { A() {} ~A() {} union { vector vi; vector db
考虑下面的代码,我已经写了: #include #include union myAccess { uint16_t access16; struct { uint
我想弄清楚你从 C99 中对齐变量的地役权中得到了什么: Exception to strict aliasing rule in C from 6.5.2.3 Structure and union
我正在通过 UNION 或 UNION ALL 从多个表中选择一列外键。 当重复无关紧要时,通常建议使用 UNION ALL 而不是 UNION 来解决性能问题。但是,在我的调用 PHP 脚本中,循环
在 C++ 中,union 可以包含静态成员,在类的情况下,这些成员属于一个类,因此对所有对象都是通用的。 union U { long l; int i; static long
任何人都可以提及普通和匿名 union (或结构)之间的区别吗?我刚找到一个: 不能在匿名 union 中定义函数。 最佳答案 您不需要点运算符“.”访问匿名 union 元素。 #include
我可能把这个复杂化了.. 我正在尝试在 Arduino 上用 C 语言为嵌入式应用程序制作一个相当可重用的分层菜单系统。我有结构来表示不同类型的菜单项,包括那些子菜单,以及这些菜单项的 union 是
我是一名优秀的程序员,十分优秀!