草庐IT

move_only

全部标签

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++ 库 "internal use only"编码风格

我正在用C++编写一个库。所有类和全局函数都在mylibrary命名空间内声明。我需要创建一些仅供内部使用的类:使用这个库的人应该立即注意到哪些类不打算在库外使用。不幸的是,我不能使用私有(private)函数,因为这会弄乱所有封装。我不能使用私有(private)类,因为内部类必须由同一个库中的“普通”类访问。我想这样做的一个好方法是创建命名空间mylibrary::internal并将所有“私有(private)”内容放入其中。这是正确的方法吗?还有其他常见的方法吗? 最佳答案 为您的内部功能使用匿名命名空间。这将确保没有外部代

c++ - 警告 : non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

这个问题在这里已经有了答案:C++ArrayInitializersWarnings(2个答案)关闭8年前。我刚刚在我的机器上安装了最新版本的cygwin和eclipseluna。它工作正常,我能够运行我的项目。但是,当我构建它们时,我收到了我不明白的警告。例如,这是我从“c++Primer”一书的网站上获得的头文件“Sales_item.h”的警告:warning:defaultedanddeletedfunctionsonlyavailablewith-std=c++11or-std=gnu++11[enabledbydefault]Sales_item()=default;^..

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