草庐IT

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++ - WM_KEYDOWN 困惑

我试图让我的应用程序在按下CTRL+S时执行某些操作。我只是不确定W和L参数如何为WM_KEYDOWN工作。MSDN有一些关于位域的内容,我不确定。如何检测CTRL和S?谢谢如果除了hWnd之外的另一个控件获得焦点,我该怎么办? 最佳答案 嗯,this是虚拟键码的大列表。CTRL-S将作为2个WM_KEYDOWN消息发送-按下ctrl键时的消息(VK_LCONTROL或VK_RCONTROL)后跟“S”键的0x53。与其处理这两条消息,不如等待“S”按下的按键消息,然后调用GetKeyState使用魔术值VK_CONTROL(否则您

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

c++ - std::move 是否使指针无效?

假设如下:templateclassPipeline{[...]voidconnect(OutputSidefirst,InputSidesecond){Queuequeue;first.setOutputQueue(&queue);second.setInputQueue(&queue);queues.push_back(std::move(queue));}[...]std::vector>queues;};move后指向队列的指针在“first”和“second”中是否仍然有效? 最佳答案 Doesstd::moveinval

c++ - valgrind Conditional jump or move depends on uninitialised value(s) ,这是否表示内存泄漏?

我在代码中遇到内存泄漏问题,在它运行时,堆不断增加到最大值,我需要重新启动服务,我运行了top命令,看到每当我调用一个场景时堆都在增加服务。我用valgrind运行服务,valgrind--log-file=log-feb19.txt--leak-check=full--show-reachable=yes--track-origins=yesmyservice我在运行场景时没有看到任何明显丢失或可能丢失的block,但我看到很多条件跳转或移动取决于未初始化的值错误。这些是否算作内存泄漏?我得到的例子:==27278==Conditionaljumpormovedependsonuni

c++ - 将对象作为函数参数发送时 move 语义

我在玩move构造函数和move赋值时偶然发现了这个问题。第一段代码:#include#includeclassFoo{public:Foo(){}Foo(Foo&&other){value=std::move(other.value);other.value=1;//sinceit'sint!}intvalue;private:Foo(constFoo&other);};voidBar(Foo&&x){std::cout在我看来,当我使用:Bar(std::move(foo));程序应将foo对象“move”到使用Bar函数中的move构造函数创建的临时对象。这样做会使foo对象的值

c++ - 如何 move std::ostringstream 的底层字符串对象?

#include#includeusingnamespacestd;templatestringToString(constT&obj){ostringstreamoss;oss如何movestd::ostringstream的底层字符串对象? 最佳答案 标准说std::ostringstream::str()returnsacopy.避免这种复制的一种方法是实现另一个直接公开字符串缓冲区的std::streambuf派生类。Boost.IOStreams使这变得非常简单:#include#include#includenamesp