草庐IT

move_disc

全部标签

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中有描述:当满足复制操作的省略标准或除了源对象是函数参数并且要

c++ - 在 move 构造过程中,从对象 move 的基本类型成员的值可以说些什么?

考虑这段代码Foof1;Foof2{std::move(f1)};我希望f1的成员值不再一定包含默认构造函数给出的值。但是,使用Foo的这种实现对多个编译器进行的测试表明情况并非如此。classFoo{public:Foo()=default;Foo(Foo&&)=default;std::strings{"foo"};boolt{true};boolf{false};};move后f1.t始终为true而f1.f始终为false。如thisquestion中所述我希望这些值要么是不确定的,要么两个bool值具有相同的值。但是,它们似乎获得了与默认构造函数相同的值。Liveexampl

c++ - 在具有 move 语义的 RAII 类中,默认构造函数应该做什么?

move语义非常适合RAII类。它们允许人们像拥有值(value)语义一样进行编程,而无需付出大量复制的代价。一个很好的例子是returningstd::vectorfromafunction.然而,使用值语义编程意味着,人们会期望类型表现得像原始数据类型。这两个方面有时似乎是矛盾的。一方面,在RAII中,人们会期望默认构造函数返回一个完全初始化的对象,或者如果资源获取失败则抛出异常。这保证了任何构造的对象都将处于有效且一致的状态(即可以安全使用)。另一方面,对于move语义,当对象位于validbutunspecifiedstate中时存在一个点.同样,原始数据类型可以处于未初始化状