草庐IT

nothrow_move_constructible

全部标签

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++ - 为什么我需要在 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&

c++ - "Move or throw"使用三元运算符

自C++11以来,我一直在使用三元运算符根据某些条件移动或抛出,但最新的GCC(9.1和树干)不再工作。我已将问题简化为这个例子(Wandboxpermalink):#include#includeintmain(){autop=std::make_unique();std::cout它适用于GCC8.3及更早版本,以及每个Clang版本;并且p被移动:p.get():0xde5c20Movepintoqq.get():0xde5c20p.get():0但现在使用GCC9.1及更高版本它不起作用:p.get():0x1d89150Movepintoqq.get():0x1d89150p

c++ - "Move or throw"使用三元运算符

自C++11以来,我一直在使用三元运算符根据某些条件移动或抛出,但最新的GCC(9.1和树干)不再工作。我已将问题简化为这个例子(Wandboxpermalink):#include#includeintmain(){autop=std::make_unique();std::cout它适用于GCC8.3及更早版本,以及每个Clang版本;并且p被移动:p.get():0xde5c20Movepintoqq.get():0xde5c20p.get():0但现在使用GCC9.1及更高版本它不起作用:p.get():0x1d89150Movepintoqq.get():0x1d89150p

c++ - clang 5:std::optional 实例化参数类型的 std::is_constructible 特征

当切换到c++17并用标准解决方案替换自定义std::optional解决方案时,检测到clang5的一个非常奇怪和意外的行为。出于某种原因,由于对参数类的std::is_constructible特征的错误评估,emplace()被禁用。在复制之前必须满足一些特定的先决条件:#include///Precondition#1:TmustbeanestedstructstructFoo{structVictim{///Precondition#2:Tmusthaveanaggregate-initializer///foroneofitsmembersstd::size_tvalue{