15得票2回答
C++中的复制/移动构造函数选择规则是什么?何时会发生移动到复制的回退?

第一个例子:#include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; A(const A&) = delet...

14得票1回答
移动构造函数未按预期被调用

我刚接触C++0x,正试图理解右值引用和移动构造函数。我正在使用带有-std=c++0x选项的g++ 4.4.6,并对以下代码感到困惑: class Foo { public: Foo() : p( new int(0) ) ...

14得票2回答
为什么在移动构造函数的初始化列表中需要使用std::move?

假设我有一个(微不足道)的类,它可以进行移动构造和移动赋值,但不能进行复制构造或复制赋值: class movable { public: explicit movable(int) {} movable(movable&&) {} movable...

14得票2回答
使用移动构造函数时,是否仍然遵守不在异常中嵌入std::string的规则?

我之前听说不应该创建带有std::string类型字段的异常类。这也是Boost网站的说法。原因在于,std::string的复制构造函数可能会在内存分配失败时抛出异常,如果在当前处理的异常被捕获之前抛出异常,程序就会终止。 不过,在移动构造函数的世界中,这个说法还适用吗?如果抛出异常时使用...

14得票4回答
Visual Studio 2017需要显式声明移动构造函数吗?

以下代码可在Visual Studio 2015中成功编译,但在使用Visual Studio 2017时失败。Visual Studio 2017报告如下错误: 错误 C2280:“std::pair::pair(const std::pair &)”:尝试引用已删除的函数 代码...

12得票3回答
偷窃移动构造函数内部的数据

在实现一个玩具类的移动构造函数时,我注意到了一个模式:array2D(array2D&& that) { data_ = that.data_; that.data_ = 0; height_ = that.height_; that.heig...

12得票2回答
使用析构函数和移动构造函数实现移动赋值

假设我有一个管理内存的类,因此需要用户定义特殊成员函数(类似于vector或类似的容器)。 考虑下面的移动赋值运算符实现:Class& operator=(Class&& rhs) { this->~Class(); ...

12得票1回答
为什么C++默认不使用移动构造函数来处理右值引用?

假设我有以下函数void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } 为什么我需要使用std::move()将para...

12得票2回答
为什么当T类型无法进行移动构造时,std::optional的移动构造函数没有被删除?

根据标准,std::optional<T> 的拷贝构造函数: ...如果 is_copy_constructible_v<T> 为 true,则定义为删除。 但是,std::optional<T> 的移动构造函数: ...如果 is_move_c...

12得票6回答
C++11中将元素移出std priority_queue

最小化的工作示例。#include <cassert> #include <list> #include <queue> //#define USE_PQ struct MyClass { const char* str; MyClass(...