草庐IT

move-constructor

全部标签

c++ - std::move 已经是 T&& 的变量

在页面Howto:WriteaMoveConstructorMicrosoft有一个关于如何编写移动构造函数的示例。它本质上是这样的:MyClass::MyClass(MyClass&&lhs){*this=std::move(lhs);}我已经尝试过了,这里确实需要std::move,但为什么呢?我认为move所做的唯一一件事就是转换为T&&。但是lhs已经是MyClass&&类型了,不是吗? 最佳答案 Namedrvaluereferencesarelvalues.Unnamedrvaluereferencesarervalue

c++ - std::move 已经是 T&& 的变量

在页面Howto:WriteaMoveConstructorMicrosoft有一个关于如何编写移动构造函数的示例。它本质上是这样的:MyClass::MyClass(MyClass&&lhs){*this=std::move(lhs);}我已经尝试过了,这里确实需要std::move,但为什么呢?我认为move所做的唯一一件事就是转换为T&&。但是lhs已经是MyClass&&类型了,不是吗? 最佳答案 Namedrvaluereferencesarelvalues.Unnamedrvaluereferencesarervalue

c++ - 了解 `std::is_move_constructible`

没有移动构造函数但具有接受constT&参数的复制构造函数的类型,满足std::is_move_constructible。例如,在以下代码中:#includestructT{T(constT&){}//T(T&&)=delete;};intmain(){static_assert(std::is_move_constructible::value,"notmoveconstructible");return0;}T将没有隐式移动构造函数,因为它有一个用户定义的复制构造函数。但是,如果我们取消注释移动构造函数的显式删除,代码将不再编译。为什么是这样?我本来希望显式复制构造函数仍然满足s

c++ - 了解 `std::is_move_constructible`

没有移动构造函数但具有接受constT&参数的复制构造函数的类型,满足std::is_move_constructible。例如,在以下代码中:#includestructT{T(constT&){}//T(T&&)=delete;};intmain(){static_assert(std::is_move_constructible::value,"notmoveconstructible");return0;}T将没有隐式移动构造函数,因为它有一个用户定义的复制构造函数。但是,如果我们取消注释移动构造函数的显式删除,代码将不再编译。为什么是这样?我本来希望显式复制构造函数仍然满足s

c++ - Visual Studio 2017 是否需要显式移动构造函数声明?

使用VisualStudio2015可以成功编译以下代码,但使用VisualStudio2017编译失败。VisualStudio2017报告:errorC2280:“std::pair::pair(conststd::pair&)”:attemptingtoreferenceadeletedfunction代码#include#includestructNode{std::unordered_map>map_;//UncommentingthefollowingtwolineswillpassVisualStudio2017compilation//Node(Node&&o)=def

c++ - Visual Studio 2017 是否需要显式移动构造函数声明?

使用VisualStudio2015可以成功编译以下代码,但使用VisualStudio2017编译失败。VisualStudio2017报告:errorC2280:“std::pair::pair(conststd::pair&)”:attemptingtoreferenceadeletedfunction代码#include#includestructNode{std::unordered_map>map_;//UncommentingthefollowingtwolineswillpassVisualStudio2017compilation//Node(Node&&o)=def

c++ - 为什么我需要在 move 构造函数的初始化列表中使用 std::move?

假设我有一个(平凡的)类,它可以move构造和move分配,但不能复制构造或复制分配:classmovable{public:explicitmovable(int){}movable(movable&&){}movable&operator=(movable&&){return*this;}movable(constmovable&)=delete;movable&operator=(constmovable&)=delete;};这很好用:movablem1(movable(17));这当然行不通,因为m1不是右值:movablem2(m1);但是,我可以将m1包装在std::mo

c++ - 为什么我需要在 move 构造函数的初始化列表中使用 std::move?

假设我有一个(平凡的)类,它可以move构造和move分配,但不能复制构造或复制分配:classmovable{public:explicitmovable(int){}movable(movable&&){}movable&operator=(movable&&){return*this;}movable(constmovable&)=delete;movable&operator=(constmovable&)=delete;};这很好用:movablem1(movable(17));这当然行不通,因为m1不是右值:movablem2(m1);但是,我可以将m1包装在std::mo

c++ - 我应该在移动构造函数中 std::move 一个 shared_ptr 吗?

考虑:#include#include#include#include#include#includeusingnamespacestd;classGizmo{public:Gizmo():foo_(shared_ptr(newstring("bar"))){};Gizmo(Gizmo&&rhs);//ImplementedBelowprivate:shared_ptrfoo_;};/*//doesn'tusestd::moveGizmo::Gizmo(Gizmo&&rhs):foo_(rhs.foo_){}*///Doesusestd::moveGizmo::Gizmo(Gizmo&

c++ - 我应该在移动构造函数中 std::move 一个 shared_ptr 吗?

考虑:#include#include#include#include#include#includeusingnamespacestd;classGizmo{public:Gizmo():foo_(shared_ptr(newstring("bar"))){};Gizmo(Gizmo&&rhs);//ImplementedBelowprivate:shared_ptrfoo_;};/*//doesn'tusestd::moveGizmo::Gizmo(Gizmo&&rhs):foo_(rhs.foo_){}*///Doesusestd::moveGizmo::Gizmo(Gizmo&