草庐IT

c++ - 通过 destruct+move 构造的 move 分配安全吗?

这里有一个非常简单的方法来为大多数带有move构造函数的类定义move赋值:classFoo{public:Foo(Foo&&foo);//youstillhavetowritethisoneFoo&operator=(Foo&&foo){if(this!=&foo){//avoiddestructingtheonlycopythis->~Foo();//callyourowndestructornew(this)Foo(std::move(foo));//callmoveconstructorviaplacementnew}return*this;}//...};在标准C++11中,

c++ - move 构造函数不能默认

我是c++11的新手并编写了以下类,我希望它支持std::move:classX{public:X(intx):x_(x){}~X(){printf("X(%d)hasbereleased.\n",x_);}X(X&&)=default;X&operator=(X&&)=default;X(constX&)=delete;X&operator=(constX&)=delete;private:intx_;};但是,当我使用选项-std=c++0x编译它时,编译器会报错如下:queue_test.cc:12:error:‘X::X(X&&)’cannotbedefaultedqueue_

c++ - move 分配给自己的行为

这个问题在这里已经有了答案:Whatdoesthestandardlibraryguaranteeaboutselfmoveassignment?(2个答案)关闭8年前。示例代码:#includeintmain(){std::vectorw(20,123),x;w=std::move(w);std::coutg++4.8.3上的输出:0当然,标准规定move赋值运算符使操作数处于未指定状态。例如,如果代码是x=std::move(w);那么我们期望w.size()为零。但是,是否有特定的顺序或其他条款涵盖自行搬家的情况?size是否为0未说明或20,或其他东西,或未定义的行为?标准容器

c++ - 默认 move 构造函数和引用成员

来自N3337的[12.8][11]:Theimplicitly-definedcopy/moveconstructorforanon-unionclassXperformsamemberwisecopy/moveofitsbasesandmembers.[Note:brace-or-equal-initializersofnon-staticdatamembersareignored.Seealsotheexamplein12.6.2.—endnote]Theorderofinitializationisthesameastheorderofinitializationofbases

c++ - 使用不可复制(但可 move )键 move 分配 map 时出错

为什么这不起作用:#include#includestd::map,std::unique_ptr>foo();std::map,std::unique_ptr>barmap;intmain(){barmap=foo();return0;}虽然这样做:#include#includestd::map,std::unique_ptr>foo();std::map,std::unique_ptr>barmap;intmain(){std::map,std::unique_ptr>tmp(foo());usingstd::swap;swap(barmap,tmp);return0;}这与映射

c++ - std::move() 是否会使迭代器无效?

这个问题在这里已经有了答案:Doesmovingavectorinvalidateiterators?(4个答案)关闭5年前。考虑以下程序:structlist_wrapper{std::vectorm_list;};intmain(){std::vectormyList{1,1,2,3,5};conststd::vector::iteratoriter=myList.begin();list_wrapperwrappedList;wrappedList.m_list=std::move(myList);//CanIstilldereferenceiter?return0;}调用std

c++ - 如何将不可复制和不可 move 的对象分配到 std::map 中?

我有一个对象,我想将其限制为仅在std::map内部分配。这是简化的代码:#includeclassValue{public:Value(intvalue){_value=value;}Value(constValue&)=delete;Value&operator=(constValue&)=delete;Value(Value&&)=default;//***void*operatornew(size_t)=delete;//notonfreestoreprivate:int_value;};classContainer{public:Container();Value*addTo

c++ - 为什么我必须在右值引用上调用 move?

在下面的代码中,为什么第一个调用mkme=mvme_rv不分派(dispatch)给T&operator=(constT&&)?#include#include#includeusingnamespacestd;usingT=vector;intmain(){Tmvme(10,1),mkme;T&&mvme_rv=move(mvme);//rvalueref?mkme=mvme_rv;//callsT&operator=(constT&)?cout 最佳答案 正如skypjack正确评论的那样,通过名称访问对象总是会产生左值引用。这

c++ - 自动检测C++14 "return should use std::move"情况

我的理解是,在C++17中,以下代码片段旨在做正确的事:structInstrument;//instrumented(non-trivial)moveandcopyoperationsstructBase{Instrumenti;};structDerived:publicBase{};structUnrelated{Instrumenti;Unrelated(constDerived&d):i(d.i){}Unrelated(Derived&&d):i(std::move(d.i)){}};Unrelatedtest1(){Derivedd1;returnd1;}Basetest2

c++ - 何时在函数外部返回值使用 move 与复制?

看完这篇question.我创建了这个小测试:classA{public:A(){}A(constA&){printf("copy\n");}A(A&&){printf("move\n");}staticAf(){Aa;returna;}staticAg(){Aa;return(a);}//couldbereturn*&a;too.staticAh(){Aa;returntrue?a:a;}};结果是(没有RVO和NRVO):f使用moveg使用moveh使用拷贝据我所知,用于决定是使用复制还是move的规则在12.8.32中有描述:当满足复制操作的省略标准或除了源对象是函数参数并且要