草庐IT

c++ - 从 r-value ref-qualified 方法 move 还是不 move ?

在以下C++11+代码中,应该首选哪个return语句构造?#includestructBar{};structFoo{Barbar;Barget()&&{returnstd::move(bar);//1returnbar;//2}}; 最佳答案 好吧,既然它是一个r-valueref限定的成员函数,this大概就要过期了。因此,将bar移出是有意义的,假设Bar实际上从被move中获得了一些东西。由于bar是一个成员,而不是本地对象/函数参数,因此在return语句中复制省略的常用标准不适用。除非您明确地std::move它,否则

c++ - 私有(private)构造函数禁止使用 emplace[_back]() 以避免 move

考虑以下代码:#includeclassA{public:A(A&&);//somewhatexpensivestaticstd::vectormake_As(){std::vectorresult;result.push_back(A(3));result.push_back(A(4));returnresult;}private:A(int);//privateconstructor};自从A的move构造函数有点昂贵(无论出于何种原因),我想避免调用它并使用emplace_back()而是:#includeclassA{public:A(A&&);//somewhatexpens

c++ - 我需要将 std::move 移植到我的内核吗?

我担心在内核领域我将无法访问std::move、std::forward、std::initializer_list之类的东西等。虽然其中一些功能内置在语言中,但它们仍然需要适当的header和库实现。以下内容是否足以利用移动语义,还是我需要走完整的九码并移植C++库?templatetypenameremove_reference::type&&move(T&&arg){returnstatic_cast::type&&>(arg);} 最佳答案 假设没有错别字,应该可以。标准中给出了完整的定义(实际上是两个;旧的和新的),我已经

c++ - 通用转换运算符模板和 move 语义 : any universal solution?

这是Explicitref-qualifiedconversionoperatortemplatesinaction的后续事件.我已经尝试了许多不同的选项,我在这里给出了一些结果,试图看看最终是否有任何解决方案。假设一个类(例如any)需要以一种方便、安全(毫无意外)的方式提供对任何可能类型的转换,同时保留move语义。我能想到四种不同的方法。structA{//explicitconversionoperators(nice,safe?)templateexplicitoperatorT&&()&&;templateexplicitoperatorT&()&;templateexpl

c++ - 如果成员具有非平凡的 noexcept 赋值运算符,则默认 move 赋值不能显式地为 noexcept

此代码使用gcc4.8.2(-std=c++11)编译失败,但使用clang3.4(trunk)(-std=c++11)编译:#include#includestructX{X&operator=(X&&)noexcept=default;//addingnoexceptthisleadstoanerroringcc,butworksinclang://function‘X&X::operator=(X&&)’defaultedonitsfirst//declarationwithanexception-specificationthatdiffersfromthe//implicit

c++ - 为什么要明确 move 转发引用?

我正在查看一些代码,我看到以下函数:templatestaticreturn_tmake_return(Args&&...args){//usingstd::forwardwillpreservelvalueargsassuch,butthepointofthisfunction//istomakeareturn,wherethe99.9+%caseismovingalocal(lvalue)intothereturnpack.//Thusitforcesamove,whichwillmove`T&`args(but_not_`constT&`args)intothe//returnp

c++ - 不可 move -不可复制对象的 vector 的 move 分配不编译

以下代码无法使用VisualStudio2013编译:#includestructX{X()=default;X(constX&)=delete;X&operator=(constX&)=delete;X(X&&)=delete;X&operator=(X&&)=delete;~X()=default;};voidfoo(){std::vectorv;std::vectorw;w=std::move(v);}错误信息说errorC2280:'X::X(X&&)':attemptingtoreferenceadeletedfunction这对我来说毫无意义。您应该不需要X的move构造函

c++ - move 分配和引用成员

对具有引用成员变量的类进行复制分配是禁忌,因为您无法重新分配引用。但是move分配呢?我尝试简单地move,但是当我只想move引用本身时,这当然会破坏源对象:classC{public:C(X&x):x_(x){}C(C&&other):x_(std::move(other.x_)){}C&operator=(C&&other){x_=std::move(other.x_);}private:X&x_;};Xy;Cc1(y);Xz;Cc2(z);c2=c1;//destroysyaswellasz我不应该只实现move分配并坚持move构造吗?这使得swap(C&,C&)难以实现。

c++ - 是否有可复制但不可 move 的类的用例?

看完thisrecentquestion@Mehrdad关于应该使哪些类不可move因此不可复制,我开始想知道是否有一个类的用例可以复制但不能move.从技术上讲,这是可能的:structS{S(){}S(Sconst&s){}S(S&&)=delete;};Sfoo(){Ss1;Ss2(s1);//OK(copyable)returns1;//ERROR!(non-movable)}虽然S有一个复制构造函数,但它显然没有模拟CopyConstructible的概念,因为那又是对MoveConstructible的改进概念,它需要存在(未删除的)move构造函数(参见第17.6.3.1

c++ - 编译器何时应该生成 move 构造函数?

我使用VS11并使用以下内容:classContextWrapper{public:ContextWrapper(){}//itshouldbedefaultedI*guess*inordertohaveautomaticmoveconstructor?//nosupportinVS11forthatnowContext*GetContext(){returnthis->context.get();}voidSetContext(std::unique_ptrcontext){this->context=std::move(context);}//ContextWrapper(Cont