草庐IT

move_member

全部标签

c++ - std::move() 或其在局部变量上的显式等效项是否允许省略?

例如:Bigcreate(){Bigx;returnstd::move(x);//returnstatic_cast::type&&>(t)//whynotelidehere?}假设应用std::move()返回局部变量会抑制move语义,因为编译器通常无法对函数的内部工作做出任何假设,如果不需要这些假设,例如当:std::move(x)是内联的(可能总是)std::move(x)写成:static_cast::type&&>(t)根据当前标准,允许实现应用NRVO...—inareturnstatementinafunctionwithaclassreturntype,whenthe

c++ - QT 插槽 : Pointer to Member Function error

我目前正在做一个Qt项目,我在SLOT方面遇到了一些麻烦。我想将一个指向成员函数的指针作为SLOT的参数传递。为此,我在类里面声明了SLOT,但是当我这样做时,出现了MOC错误。我不知道我想要实现的目标是否有可能实现。名为MainFrame的类的语法示例:voidslotAnswerReceived(QStringanswer,void(MainFrame::*ptr)(QString));我在任何地方都没有任何连接,没有任何东西使用该功能,我遇到的唯一错误是上面这一行。谢谢大家的帮助。我在网上找不到任何解决方案(但我发现这篇文章解释了SIGNALandSLOTindepth,如果有人

C++ : Connecting a member of a class to its definition

我是C++的新手,我在OCaml和Python方面有更多经验。我想通过制作一个玩“MorpionSolitaire”的程序来学习C++。我的开始有点困难。在下面的代码中:typedefenum{NORTH,NORTHEAST,EAST,SOUTHEAST}direction;chardeltax[4]={0,1,1,1};chardeltay[4]={1,1,0,-1};classCoords{private:charx,y;public:Coords(charxx,charyy){x=xx;y=yy;};charget_x()const{returnx;}charget_y()con

c++ - 结构绑定(bind) : binding to public data members (inherited base class )

即使基类和派生类都有公共(public)数据成员#includeclassM{public:intx=2;volatiledoubley=3;};classS:publicM{public:intx1=4;volatiledoubley1=5;};intmain(){Sf();Sa;std::cout获取错误auto[b,c,d,e]=f();main.cpp:Infunction'intmain()':main.cpp:21:10:error:cannotdecomposeclasstype'S':bothitanditsbaseclass'M'havenon-staticdatam

c++ - 从 STL 容器中 move 元素是否会将其从该容器中移除?

我有一个Foobar类,它带有一个输出“Wellhellothere!”的sayHello()方法。如果我写下面的代码vector>fooList;fooList.emplace_back(newFoobar());unique_ptrmyFoo=move(fooList[0]);unique_ptrmyFoo2=move(fooList[0]);myFoo->sayHello();myFoo2->sayHello();cout输出是:Wellhellothere!Wellhellothere!vectorsize:1我很困惑为什么会这样。当我迈出第一步时,fooList[0]不应该变

c++ - 抽象类 : invalid abstract return type for member function ‘virtual...’

在我的程序中我有这样的类层次结构:#include#include#includeusingnamespacestd;classaa;classbb;classroot{public:virtual~root(){}virtualrootadd(constaa&a)const=0;virtualrootadd(constbb&a)const=0;};classaa:publicroot{public:aa(){}aa(constaa&a){}virtualrootadd(constaa&a)const{returnroot(newaa());}virtualrootadd(constb

C++11:按值调用、 move 语义和继承

假设我有一个类,我打算将其直接公开为可实例化的类对程序员:classBase{public:Base(std::stringtext):m_text(std::move(text)){}private:std::stringm_text;};到目前为止一切顺利。这里不需要右值构造函数。现在,在未来的某个时刻,我决定扩展Base:classDerived:publicBase{public:Derived(conststd::string&text):Base(text){}};这让我很烦恼:我不能在Derived中按值获取字符串,因为那是Base已经在做-我最终会得到2个拷贝和1个mo

c++ - std::move 和 RVO 优化

我最近读到std::move如何通过移动值而不是复制它们来加速代码。所以我做了一个测试程序来比较使用std::vector的速度。代码:#include#include#include#ifdefWIN32#include#else#include#include#endif#undefmax//ReturnstheamountofmillisecondselapsedsincetheUNIXepoch.Worksonboth//windowsandlinux.uint64_tGetTimeMs64(){#ifdef_WIN32//WindowsFILETIMEft;LARGE_INT

c++ - 有没有办法在不使用 std::move 的情况下用构造类初始化类?

//Exampleprogram#include#include#includeclassA{public:intx;};classB{public:B(A&&a):m_a(std::move(a)){}Am_a;};intmain(){Bvar(std::move(A()));//Bvar(A());//doesnotcompilewhy?std::cout在上面的代码片段中,被注释掉的行无法编译。错误消息似乎将var视为函数声明。即使A具有构造函数的参数,它仍然被视为函数声明。有没有一种方法可以将它写成不被视为函数声明?在这种情况下,使用typename没有帮助。

c++ - C++ 对象没有得到 std::moved 吗?

我得到以下代码:(CodeLive:C++Shell)classWorker{public:Worker(std::stringname):_name(name){};Worker(constWorker&worker):_name(worker._name){std::cout_name=name;}private:std::string_name;};classFactory{public:Factory(){std::coutworkers.push_back(std::move(worker));}Worker&getLastWorker(){this->workers.bac