草庐IT

c++ - std::move_if_noexcept 的基本原理仍在 move 抛出仅 move 类型?

move_if_noexcept将:返回一个右值——促进move——如果move构造函数是noexcept或者如果没有复制构造函数(仅move类型)返回一个左值——强制复制——否则我发现这相当令人惊讶,因为具有抛出move-ctor的仅move类型仍将由使用move_if_noexcept的代码调用此move-ctor。是否对此给出了详尽的理由?(也许直接或在N2983的两行之间?)代码不编译而不是仍然不得不面对不可恢复的move场景会不会更好?N2983中给出的vector示例很好:voidreserve(size_typen){......new((void*)(new_begin

c++ - 组合两个复制和 move 的构造函数

目前,我的一个玩具类模板有两个看起来非常相似的构造函数:optional(constT&x){construct(x);}optional(T&&x){construct(std::move(x));}我能否将它们组合成一个构造函数模板,或者这会以某种方式改变语义吗?templateoptional(U&&x){construct(std::forward(x));} 最佳答案 抱歉,模板构造函数永远不会(被编译器认为是)复制构造函数。 关于c++-组合两个复制和move的构造函数,我们

c++ - 自 C++17 的类模板参数推导以来,std::make_move_iterator 是否多余?

从C++11开始,要将一些vectory附加到另一个vectorx,您可以这样做:x.insert(x.end(),std::make_move_iterator(y.begin()),std::make_move_iterator(y.end()));使用C++17类模板参数推导,可以更简洁地编写此代码:x.insert(x.end(),std::move_iterator(y.begin()),std::move_iterator(y.end()));从C++17开始,这不会使std::make_move_iterator变得多余吗?std::make_move_iterator(

c++ - std::move( ) 在没有 move-ctor 的情况下调用 copy-ctor。为什么以及如何预防?

我想知道是否有一种安全编程实践可以在这种微妙的行为发生时提醒编码人员,或者更好的是,首先避免这种行为。structA的用户可能没有意识到没有move构造函数。在他们尝试调用不存在的ctor时,他们既没有收到编译器警告,也没有收到任何运行时指示调用了复制ctor。下面的答案解释了发生的转换,但我看不出这是一件好事的任何理由。如果缺少以const引用作为参数的构造函数,则会出现编译时错误,而不仅仅是解析为非常量引用版本。那么,当类中没有实现move语义时,为什么尝试使用move语义不会导致编译时错误?有没有办法通过一些编译时选项避免这种行为,或者至少有一种在运行时检测它的方法?在move之

c++ - 为什么 move on const 对象有效?

我有一个简单的代码:conststd::vectordata={1,2,3};std::vectordata_moved=std::move(data);for(auto&i:data)cout编译没有任何错误或警告!!并且data中似乎仍有值!moveconst值似乎不正确,因为我们无法修改const对象那么该代码如何编译?! 最佳答案 您没有move任何东西。std::move名字真的很糟糕:它不会强制move;它只是返回一个右值。std::vector的构造函数由编译器决定。调用,而这决定了您是否会采取行动。如果由于目标的mo

c++ - move 的对象仍然被破坏?

在学习C++11时,我对move对象的行为方式感到惊讶。考虑这段代码:#include#include#includeclassMoveable{public:Moveable(){std::cout::value,"isnotcopyconstructible");static_assert(!std::is_copy_assignable::value,"isnotcopyassignable");static_assert(std::is_move_constructible::value,"ismoveconstructible");static_assert(std::is_

c++ - 返回 const 值以利用 move 语义与防止诸如 (a+b)=c 之类的东西

这个问题在这里已经有了答案:Isn'ttheconstmodifierhereunnecessary?[duplicate](5个答案)关闭8年前。我认为thisquestion有点被误解了。返回const值并不是可以被视为无意义的东西。正如AdamBurry在评论中指出的那样,ScottMeyers在更有效的C++(第6项)中推荐了它,我将向其中添加HerbSutter的ExceptionalC++(第20项,类力学,其对应的GotW为availableonline)。这样做的基本原理是您希望编译器捕获像(a+b)=c(哎呀,意思是==)这样的拼写错误,或误导性语句像a++++,这两

c++ - std::move 与 std::make_pair

有什么区别:std::map>m;Tt1,t2;m.emplace(1,std::make_pair(t1,t2));和:std::map>m;Tt1,t2;m.emplace(1,std::move(std::make_pair(t1,t2)));std::move在这里是多余的吗?std::map::emplace和perfectforwarding是否负责直接在std::中分配std::pairmap? 最佳答案 std::make_pair(...)和std::move(std::make_pair(...))都是右值表达式

c++ - 为什么 std::vector 使用 move 构造函数,尽管声明为 noexcept(false)

无论我在互联网上的什么地方阅读,强烈建议如果我希望我的类(class)与std::vector一起工作(即std使用我类(class)的move语义::vector)我应该将构造函数delcaremove为'noexcept'(或noexcept(true))。为什么std::vector使用它,即使我将它标记为noexcept(false)作为实验?#include#includeusingstd::cout;structT{T(){coutt_vec;t_vec.push_back(T());}输出:T()T(T&&)~T()~T()为什么?我做错了什么?在gcc4.8.2上编译,

c++ - 你能重用 move 的 std::string 吗?

这个问题在这里已经有了答案:Reusingamovedcontainer?(3个答案)关闭8年前。给出这个例子:std::vectorsplit(conststd::string&str){std::vectorresult;std::stringcurr;for(autoc:str){if(c==DELIMITER){result.push_back(std::move(curr));//ATTENTIONHERE!}else{curr.push_back(c);}}result.push_back(std::move(curr));returnresult;}我可以重用currst