草庐IT

c++ - 为什么在此示例中必须使用 emplace_back move ?

以下最小工作示例在使用选项1或选项2下的代码时编译,但在使用选项3下的代码时不编译。我假设emplace_back()隐式使用/调用move构造函数,那么为什么需要显式move()呢?它与r-value和l-value有关系吗?或者这是否与需要转让所有权的std::unique_ptr有关?(我对这些概念还是陌生的,尤其是在这种情况下。)为了完整性,带有push_back()的选项4也不会编译,除非调用move()。#include#include#includeclassBeta{public:Beta(intx,inty,intz):mX(x),mY(y),mZ(z){};intm

c++ - 在哪里使用 std::move 来处理字符串?

我正在读这个post.我找到了以下代码。我在想:std::move对字符串有用吗(假设字符串足够长)?它是否使之前的字符串无效?我应该在什么地方使用它,在什么地方不应该使用它?.className{public:Name(std::stringfirstName,std::stringlastName):firstName_(std::move(firstName)),lastName_(std::move(lastName)){}voidprint()const{std::cout我的技术一直在使用constructor(conststd::string&argument):fiel

c++ - 在 std::pair 中存储不可复制(但可 move )的对象

我正在尝试将不可复制(但可move)的对象存储在std::pair中,如下所示:#includestructS{S();private:S(constS&);S&operator=(constS&);};intmain(){std::pairp{0,S()};return0;}但是我在使用gcc4.6时遇到以下编译器错误:Infileincludedfrominclude/c++/4.6.0/bits/move.h:53:0,frominclude/c++/4.6.0/bits/stl_pair.h:60,include/c++/4.6.0/utility:71,fromsrc/tes

c++ - move 构造函数问题

我有以下类(class):classStudent{private:std::stringfirstName;std::stringlastName;public:Student():firstName(""),lastName(""){}Student(conststd::string&first,conststd::string&last):firstName(first),lastName(last){}Student(constStudent&student):firstName(student.firstName),lastName(student.lastName){}St

c++ - 实现 move 构造函数如何影响返回值优化?

考虑以下代码片段:#include#includeclassA{public:A(){std::cout它用g++和clang++编译得很好,输出是A::A()A::~A()在这种情况下,RVO似乎开始发挥作用。请注意,没有调用move构造函数。但是,如果从上面的代码中删除那个未使用的move构造函数,那么片段会变成这样:#include#includeclassA{public:A(){std::coutclang++和g++都拒绝编译它,因为类A的复制构造函数被标记为已删除,所以似乎没有发生RVO。如何删除未使用的move构造函数会导致这种情况? 最佳答

c++ - lambda 函数不是 throw_move_assignable 吗?

clang-cl(4.0.0-trunk)似乎认为是,而vc2015(update3)认为不是。此实现是否已定义或标准是否规定了lambda函数应如何在术语或nothrow和moveassignable中实现?#include#includetemplatevoidtest_nothrow_move_assignable(T&&){std::cout::value 最佳答案 这是clang错误。来自[expr.prim.lambda]:Theclosuretypeassociatedwithalambda-expressionhas

c++ - 为什么 std::allocator 要求 propagate_on_container_move_assignment 为真?

根据当前标准(20.7.9),std::allocator有一个成员propagate_on_container_move_assignment设置为true_type:templateclassallocator{public:typedefsize_tsize_type;typedefptrdiff_tdifference_type;typedefT*pointer;typedefconstT*const_pointer;typedefT&reference;typedefconstT&const_reference;typedefTvalue_type;templatestruc

c++ - 从 boost multi_index 数组中 move 元素

假设我有可move且不可复制的对象,并且我有带有random_access索引的boost多索引数组。我需要将我的对象移出数组前端,但我找不到任何方法可以在documentation中给我右值/左值引用.我只能看到front()给我不断的引用和pop_front()删除元素,但不返回任何东西。那么有没有办法将元素移出boostmulti-index呢? 最佳答案 添加到@sehe的回答中,下面显示了在您的可move类型不可默认构造的情况下如何修改代码:已编辑:更改代码以正确处理*extracted的破坏。已编辑:添加了std::un

c++ - 编译器会在 setter 方法中自动应用 move 语义吗?

我想知道是否允许编译器在以下setter方法中自动使用wstring的move构造函数(无需显式调用std::move):voidSetString(std::wstringstr){m_str=str;//Willstrbemovedintom_strautomaticallyorisstd::move(str)needed?}从我读到的内容来看,似乎不允许编译器做出此决定,因为str是一个左值,但很明显,在这里使用move不会改变程序行为。除非move,是否会应用其他类型的复制省略? 最佳答案 [is]thecompiler[.

c++ - 为什么 list<unique_ptr> 中的 unique_ptr 的 std::move() 没有真正 move 它?

usingPtr=std::unique_ptr;Ptrf(boolarg){std::listlist;Ptrptr(newint(1));list.push_back(std::move(ptr));if(arg){Ptr&&obj1=std::move(list.front());//Here|obj1|and|list.front()|stillpointtothesamelocation!list.pop_front();returnstd::move(obj1);}else{Ptrobj2=std::move(list.front());list.pop_front();r