- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我注意到 move
可以应用于不与“非平凡”(不确切知道,但例如原始类型很好)成员。例如,以下代码编译(C++14,Clang):
#include <vector>
#include <string>
class Foo {
public:
union {
int i;
bool b;
};
Foo() {};
~Foo() {};
// Move constructor to default.
Foo(Foo &&) = default;
// Copy constructor deleted.
Foo(const Foo &) = delete;
};
int main() {
std::vector<Foo> v;
v.push_back(Foo());
}
注意拷贝构造函数被删除了。由于 std::vector
的 push_back
可以接受右值引用,因此在这种情况下它将使用它,并且不会发生 copy
。但是,一旦将“非平凡”类型添加到 union 中,就会强制复制构造函数 - 因此它不会编译:
#include <vector>
#include <string>
class Foo {
public:
union {
int i;
bool b;
std::string s; // <-- Added element causing compile error.
};
Foo() {};
~Foo() {};
// Move constructor to default.
Foo(Foo &&) = default;
// Copy constructor deleted.
Foo(const Foo &) = delete;
};
int main() {
std::vector<Foo> v;
v.push_back(Foo());
}
编译错误信息的相关部分:
In file included from experiment/miniso.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/vector:61:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/allocator.h:46:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0/bits/c++allocator.h:33:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/ext/new_allocator.h:136:23: error: call to deleted constructor of 'Foo'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/alloc_traits.h:475:8: note: in instantiation of function template
specialization '__gnu_cxx::new_allocator<Foo>::construct<Foo, Foo>' requested here
{ __a.construct(__p, std::forward<_Args>(__args)...); }
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/vector.tcc:100:21: note: in instantiation of function template
specialization 'std::allocator_traits<std::allocator<Foo> >::construct<Foo, Foo>' requested here
_Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/stl_vector.h:954:9: note: in instantiation of function template
specialization 'std::vector<Foo, std::allocator<Foo> >::emplace_back<Foo>' requested here
{ emplace_back(std::move(__x)); }
^
experiment/miniso.cpp:24:5: note: in instantiation of member function 'std::vector<Foo, std::allocator<Foo> >::push_back' requested here
v.push_back(Foo());
^
experiment/miniso.cpp:19:3: note: 'Foo' has been explicitly marked deleted here
Foo(const Foo &) = delete;
^
我知道一些关于什么可以 move 什么不能 move 的规则,但这似乎并不容易处理。为什么会发生这种情况?如何解决这个问题,使其不调用复制构造函数?
目标编译器是 Clang C++14。
最佳答案
它正在尝试调用您的复制构造函数,因为您的 move 构造函数已删除。哦当然,我知道你写了 = default
。但是由于 union 包含一个具有非平凡 move/复制构造函数的类型,如果用户未提供 union 的复制/move 构造函数,它将被隐式删除。
并且 = default
不会使其成为“用户提供的”。
换句话说,如果 union
的任何成员需要复制/move 代码而不是 memcpy
(又名:不可简单复制)。因此,包含 union 的类型也不能有编译器生成的复制/move 代码。在这些情况下,您必须决定如何复制/move 对象。
这样的复制/move 构造函数需要知道它是哪种类型。
关于c++ - 当 "moved"对象在 union 中有一个 "non-trivial"成员时,为什么要强制复制构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47106117/
我被要求编写一个递归代码来打印一个数组。一位 friend 向我展示了这段代码: include int i=0; void print(int A[], int n) { if(i voi
我正在尝试让 google 的示例(应用内购买 version 3)测试应用“TrivialDrive”正常工作,但遇到以下问题: 我点击“buy gas”按钮,出现以下屏幕: 此消息有两种可能的解释
我知道这是一个非常常见的主题,但尽管典型的 UB 很容易找到,但到目前为止我还没有找到这个变体。 因此,我尝试正式引入 Pixel 对象,同时避免数据的实际拷贝。 这有效吗? struct Pixel
当一个函数接受一个函数参数(或者一个类有一个函数槽)时,有两种方法可供选择: def foo(..., my_func=None, ...): ... if my_func:
你知道任何 git 命令的工具/开关可以帮助我防止冲突或加速 merge 吗? 目前我经常遇到这样的“琐碎的冲突”: ++>>>>>> 587f917... 我想这是由一些空格引起的。这很容易被人类解
我在国际象棋引擎上工作了一段时间。为了改进引擎,我编写了一些代码,将国际象棋位置从内存加载到一些调谐器代码中。我的机器上大约有 1.85B fens,加起来达到 40Gb(每个位置 24B)。 加载后
我想在 Spark 中加入两个数据集。这就是我所做的: Dataset data = spark.read().format("parquet").load("hdfs://path"); Datas
我在国际象棋引擎上工作了一段时间。为了改进引擎,我编写了一些代码,将国际象棋位置从内存加载到一些调谐器代码中。我的机器上大约有 1.85B fens,加起来达到 40Gb(每个位置 24B)。 加载后
根据我对标准的理解,普通析构函数是一种隐式声明的析构函数,其类只有基本和非静态成员以及普通析构函数。鉴于此定义的递归性,在我看来,唯一的“递归停止”条件是找到具有非隐式声明的析构函数(即用户声明的)的
在 C++0x 中,我想确定一个类是否简单/是否具有标准布局,以便我可以使用 memcpy()、memset() 等... 我应该如何使用 type_traits 实现下面的代码,这样我才能确认一个类
我有一个带有“日期”列的表,我想做一个执行以下操作的查询: 如果日期是 星期一 , 周二 , 周三 , 或 星期四 , 显示的日期应该上移 1 天,如 DATEADD(day, 1, [Date])
我有一个非常复杂的项目(大约 100 个模块),我想在它上面运行 mvn dependency:tree .它失败了,提示它无法解决的依赖关系。该项目否则编译得很好。所以我创建了我能想到的最基本的项目
我希望这会返回一个 Date 对象,表示从现在开始一小时后的时间: Calendar.current.date(byAdding: DateComponents(hour: 1), to: Date(
Blocksworld显然是自动化规划中的基准领域。 This domain consists of a set of blocks, a table and a robot hand. The bl
我是 Angular 新手,我想做一些重要的输入验证。 基本上我有一张 table 。每行包含三个文本输入。当用户输入任何文本输入时,我想检查该表是否至少包含一行具有三个非空白输入字段。如果是的话
我无法理解导致索引错误的原因,而不是寻找快速修复。但是,如果我的代码让您反感/非常无效,请告诉我。目标是生成由两个四位数的乘积产生的回文。 代码: for x in range(10000):
我有一些数据需要多个 Activity 操作。基本上有一个只读屏幕和多个编辑屏幕。 起初我考虑将数据作为字符串参数传递给 Intent,但如果用户在编辑字段后按下后退按钮,这些更改将会丢失。 那么在不
我会定义“平凡可 move ” Calling the move constructor (or the move assignment operator) is equivalent to memc
我只想 class Trivial t instance Trivial t 这在 Haskell 98 中当然没用,因为你可以忽略约束;但使用 ConstraintKinds,我们可以明确要求类型为
在 future 的 C++ 标准中,我们将拥有“平凡的可重定位性”的概念,这意味着我们可以简单地将字节从一个对象复制到未初始化的内存块,并简单地忽略/清零原始对象的字节。 这样,我们就模仿了 C 风
我是一名优秀的程序员,十分优秀!