草庐IT

c++ - 什么时候在 `std::move()` 函数中调用 move 构造函数?

函数std::move()定义为templatetypenamestd::remove_reference::type&&move(T&&t){returnstatic_cast::type&&>(t);}我可以想象要调用move构造函数的四个地方:参数传递时。执行类型转换时。返回结果时。不在std::move()函数本身,但可能在返回的引用最终到达的地方。我会赌4号,但我不能100%确定,所以请解释一下你的答案。 最佳答案 没有进行move构建。std::move()接受一个引用并返回一个引用。std::move()基本上只是一个

c++ - move 构造函数和多重继承

概要当类使用多重继承时,如何安全地设计move构造函数?详情考虑以下场景:structT{};structU{};structX:publicT,publicU{X(X&&other):T(std::move(other)),U(std::move(other))//alreadymoved?!{}};有没有办法安全地move构造T和U? 最佳答案 tl;dr:问题中的代码没问题。上面的代码很好,因为std::move本身实际上并没有以任何方式改变other,它只是做了一个转换来使other到右值引用中,以便调用T和U的move构造

c++ - P1236R1 : Why is the c++ standard trying to move away from the word "bit" when defining integers?

根据P1236R1,现在整数类型是用数字来定义的,不再是用位来定义的。typeminimumrangeexponentNsignedchar8short16int16long32longlong64C++没有定义标准仍然缺乏的“位”的含义,而是选择不这样做,而是在rangeexponent术语中定义这些类型。为什么?为什么不依赖“位”这个词比较好?该提案中的“不可观察位”是什么?P1236R1是partofC++20 最佳答案 根据ISO规则,ISOC++委员会的审议是私有(private)的,不能与整个编程社区共享。委员会已就此事

c++ - 从函数返回 const 对象会阻止从外部进行 move 构造吗?

给定这个函数和函数调用:std::stringGetString(){std::stringstreamsstr;constautostr=sstr.str();returnstr;}constautoreturnedStr=GetString();当我将str声明为const时会省略move构造吗? 最佳答案 在您的情况下,returnedStr将从GetString()的返回值move构造,但该返回值将从str复制构造(1)。如果str不是const,则返回值将从它move构造。注意,在这两种情况下,返回值优化仍然适用,因此编译

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