- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我看到的无限制 union 的例子似乎总是在构建时使用新的放置。有关 C++11 功能的维基百科文章在 union 的构造函数中使用了 new 放置。
https://en.wikipedia.org/wiki/C%2B%2B11#Unrestricted_unions
#include <new> // Required for placement '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++03; legal in C++11.
U() {new(&p) Point();} // Due to the Point member, a constructor definition is now required.
};
这里有必要用placement new吗?例如,这段代码使用 gcc 和 valgrind 编译时没有警告,当 union 用于保存字符串时,没有显示内存泄漏:
struct HasUnresUnion
{
enum { Int, String } tag;
HasUnresUnion(int i)
: tag(Int),
as_int(i)
{}
HasUnresUnion(std::string str)
: tag(String),
as_str(std::move(str))
{}
~HasUnresUnion()
{
using std::string;
if (tag == String)
as_str.~string();
}
union
{
int as_int;
std::string as_str;
};
};
这里似乎没有任何歧义,所以我不明白为什么标准会禁止这样做。这是法典吗?当 union 未初始化(而不是被分配给)时,是否需要放置新的? union 中的构造函数是否需要?我肯定见过没有自己的构造函数的不受限制的 union ,但维基百科明确指出这是必需的。
最佳答案
您显示的代码段非常安全;法律允许您在初始化类 union 类时初始化一个非静态数据成员。
维基百科文章有一个使用 placement-new 的例子,因为他们在可以直接初始化某个成员的点之后写入成员。
union A {
A () { new (&s1) std::string ("hello world"); }
~A () { s1.~basic_string<char> (); }
int n1;
std::string s1;
};
不过,前面的代码段在语义上等同于以下代码段,其中我们明确声明 A::s1
应在构造 A
时进行初始化。
union A {
A () : s1 ("hello world") { }
~A () { s1.~basic_string<char> (); }
int n1;
std::string s1;
};
在您的代码片段中,您的类中有一个匿名 union (这使您的类成为一个类 union 类),这意味着除非您初始化一个 union
的成员在类的初始化期间 — 您必须在以后使用 placement-new 来初始化它们。
9.5/1 -- <b>Unions</b> -- [class.union]p1
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.
3.8/1 -- <b>Object lifetime</b> -- [basic.life]p1
[...]
The lifetime of an object of typeT
begins when:
- storage with the proper alignment and size for type
T
is obtained, and- if the object has non-trivial initialization, its initialization is complete.
The lifetime of an object of type
T
ends when:
- if
T
is a class type with a non-trivial destructor (12.4), the destructor call starts, or- the storage which the object occupies is reused or released.
关于c++ - 不受限制的 union 是否需要放置新的和构造函数定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058717/
语句 1: [2,4,6,7,8].each do |i| (i % 2 == 0) || (puts "Not even" && break) puts i end 声明 2: [2
我有一张正在显示的卡片,上面有一些信息。我想将其包装在SingleChildScrollView中,因为我还有更多项目要添加到卡中,但是当我这样做时,屏幕只是空白吗?我曾尝试将其作为根(脚手架主体)放
我有一个带有窗体的 View ,该窗体显示ViewModel中ObservableCollection中对象的数据。 ObservableCollection使我可以浏览数据。 ObservableC
如何将时间戳附加文件名放在HDFS中? hadoop fs -put topic_2018-12-15%2016:31:15.csv /user/file_structure/ 最佳答案 您只是在运行
我正在寻求一些帮助,以找出为什么以下叠加函数的运行时间会随着每次连续运行而增加。 据我所知,如果缓冲区中的文本保持不变,则运行时间应该是相同的——即,仅向左/向右移动光标应该不会增加运行时间(但它确实
我有一个事件指示器,它显示在中间。如何将其放置在 View 的左上角? var activityIndicator = UIActivityIndicatorView() func show() {
首先,我想提前感谢所有回答这个问题的人。非常感谢您的帮助。这是我第一次在这里发帖,所以如果我发帖不礼貌,请原谅我。 我的问题是关于方法原型(prototype)的: void copySubtree(
我正在开发一个应该是通用的应用程序,一个适用于 iPad 和 iPhone 的应用程序。我想让他们的界面尽可能相似。在 iPhone 应用程序中,我使用的是选项卡栏 Controller ,其中一个选
我目前正在使用 JS 开发 REST API,但遇到以下问题:该代码有效,但如果我尝试删除、放置或修补不存在的条目,它不会返回错误,但会打印成功消息。这是为什么?获取路由完美运行。 app.route
.a{ width:500px; height:500px; background:yellow; border: 3px dashed black; }
首先,请引用下图: 这基本上是我对布局的想法。 我想要的是: 内容 div 成为“主要焦点”,例如当浏览器 调整大小,它应该留在中间; 当浏览器被调整大小时,我希望这两个图像基本上 位于内容 div
我的应用程序需要使用内存映射并发访问数据文件。我的目标是使其在共享内存系统中可扩展。研究了内存映射文件库实现的源码,想不通: 在多个线程中从 MappedByteBuffer 中读取是否合法? get
我有一个 JDesktopPane 并希望以网格样式显示 JInternalFrames 而无需覆盖框架。框架的尺寸会有所不同,因此应动态分配它们的位置。我可以存储最后放置的框架的坐标,但可以移动、最
根据https://isocpp.org/wiki/faq/dtors#placement-new传递给placement-new的地址必须正确对齐。但它给出的例子似乎与此相矛盾。 char memo
我最近一直在查看 Illumos 源代码,发现了一些奇怪的东西。 在他们的源代码中,函数类型是这样写的: static int outdec64(unsigned char *out, unsigne
您好,我目前正在尝试在我的一张图片旁边放置一个图例,但我在放置时遇到了问题。 我想将图例放在图像的左侧或右侧。这是我当前的代码: .my-legend .legend-title { text-a
根据文档, print 之间的唯一区别和 say 似乎是后者添加了 "\n"(并使用 .gist 进行字符串化)。然而, perl6 -e 'print "del\b\b"' 打印“d”,有效地应用转
所以我试图将我的图像标志放在背景上,但我的背景突然被裁剪,出现了一半的黑屏。如图: 我的 main.dart 代码: import 'package:flutter/material.dart'; i
我正在使用 Azure DevOps 构建 python 轮。我想让它尽可能通用,以便团队中的每个人都可以使用相同的管道来构建自己的 python 轮并将它们部署在一些 databricks 工作区中
在构建标准(非 WordPress)网页时,我通常会在正文末尾之前加载所有 javascript 文件,然后包含页面特定 js 代码的部分。 WorPress 建议使用 wp_enqueue_scri
我是一名优秀的程序员,十分优秀!