- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
关于这段代码:
#include <string>
int main()
{
union u {
u() { i = 0; }
~u() {}
int i;
std::string s1;
std::string s2;
} u;
new (&u) std::string{};
}
Objects can contain other objects, called subobjects. A subobject can be a member subobject ([class.mem]), a base class subobject ([class.derived]), or an array element. An object that is not a subobject of any other object is called a complete object. If an object is created in storage associated with a member subobject or array element e (which may or may not be within its lifetime), the created object is a subobject of e's containing object if:
— the lifetime of e's containing object has begun and not ended, and
— the storage for the new object exactly overlays the storage location associated with e, and
— the new object is of the same type as e (ignoring cv-qualification).
没有要求如何在与成员子对象关联的存储中创建对象。如果子对象是标准布局 union 的成员或非 union 类对象的第一个成员,则代码不必在地址运算符的参数中指定子对象。在这种情况下,获取包含对象的地址就足以指定成员子对象的存储。
«没有要求如何创建一个对象»,除此之外,意味着指向新放置的指针不必point到子对象。主要是因为可能没有对象指向(注意,[intro.object]/2 不需要子对象存在)。在 std-discussion 邮件列表中有人询问,给定一个 struct A 类型的对象
,x
{ unsigned char buf[1]; };new (&x) A{}
和 new (x.buf) A{}
有区别吗?和 the answer不是,在这两种情况下,x.buf
都会 provide storage对于 A{}
。因为
The wording in [intro.object] and [basic.life] concern themselves with the storage address represented by a pointer, not the object to which it points.
[class.union]/1发誓« union 类型对象的至多一个非静态数据成员可以在任何时候处于事件状态»。
s1
或 s2
哪个在上面的代码中变得活跃?
最佳答案
指针是一个地址,但对于对象模型而言,它不仅仅是一个地址。它指向该地址的特定对象。多个对象可以存在于某个地址,但这并不意味着指向这些对象中的任何一个的指针同时是指向该地址处其他对象的指针。考虑一下 [expr.unary.op]/1 所说的指针间接寻址:
the result is an lvalue referring to the object or function to which the expression points.
不是“那个地址的对象”;它是一个左值,指向被指向的对象。很明显,在 C++ 对象模型中,多个对象可以存在于同一地址,但指向该地址的特定指针并不指向所有这些对象。它只指向其中之一。
[expr.unary.op]/2 表示“一元 & 运算符的结果是指向其操作数的指针”。因此,&u
指向类型为 u
的 u
(顺便说一句,真的有必要将对象命名为与类型相同的名称吗? ). &u
不指向 u.i
、u.s1
或 u.s2
。所有这些都保证与 &u
共享相同的地址,但 &u
本身仅指向 u
。
那么现在的问题就变成了,&u
代表的存储是什么?好吧,根据 [intro.object]/1,我们知道“一个对象占用一个存储区域”。如果 &u
指向对象 u
,则该指针必须代表该对象占用的存储区域。不存储其任何子对象;它是该对象的存储。完整。
现在,我们进入 new(&u) std::string{}
。此表达式在 &u
表示的存储中创建一个 std::string{}
类型的对象。这表示重用对象 u
的存储。根据 [basic.life]/1.4,终止 u
的生命周期。这终止了其事件成员子对象的生命周期。
所以你的问题的答案是两者都没有激活,因为对象 u
不存在了。
关于c++ - 哪个 union 成员在安置新人后变得活跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54236936/
自从我的问题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 是
我是一名优秀的程序员,十分优秀!