草庐IT

move-constructor

全部标签

c++ - 使类不可复制*和*不可 move

在C++11之前,我可以使用它来使类不可复制:private:MyClass(constMyClass&);MyClass&operator=(constMyClass&);使用C++11,我可以这样做:MyClass(constMyClass&)=delete;MyClass&operator=(constMyClass&)=delete;当使用带有已删除拷贝和赋值的类时,是否有可能生成默认的move运算符?而且这个类并没有完全复制,而是move了(有点相似)?那么,我是否必须这样做以防止默认move构造和分配:MyClass(MyClass&&)=delete;MyClass&op

c++ - 使类不可复制*和*不可 move

在C++11之前,我可以使用它来使类不可复制:private:MyClass(constMyClass&);MyClass&operator=(constMyClass&);使用C++11,我可以这样做:MyClass(constMyClass&)=delete;MyClass&operator=(constMyClass&)=delete;当使用带有已删除拷贝和赋值的类时,是否有可能生成默认的move运算符?而且这个类并没有完全复制,而是move了(有点相似)?那么,我是否必须这样做以防止默认move构造和分配:MyClass(MyClass&&)=delete;MyClass&op

C++17 表达式求值顺序和 std::move

今天在重构一些代码以更改指向std::unique_ptr的原始指针时,我遇到了由于orderofevaluation导致的段错误错误。旧代码做了如下的事情:voidadd(conststd::string&name,Foo*f){_foo_map[name]=f;}voidprocess(Foo*f){add(f->name,f);}第一次天真地重构代码以使用std::unique_ptr:voidadd(conststd::string&name,std::unique_ptrf){_foo_map[name]=std::move(f);}voidprocess(std::uniq

C++17 表达式求值顺序和 std::move

今天在重构一些代码以更改指向std::unique_ptr的原始指针时,我遇到了由于orderofevaluation导致的段错误错误。旧代码做了如下的事情:voidadd(conststd::string&name,Foo*f){_foo_map[name]=f;}voidprocess(Foo*f){add(f->name,f);}第一次天真地重构代码以使用std::unique_ptr:voidadd(conststd::string&name,std::unique_ptrf){_foo_map[name]=std::move(f);}voidprocess(std::uniq

c++ - move 由逗号运算符抑制的构造函数

这个程序:#includestructT{T(){}T(constT&){std::coutT{returnt;})({});std::coutT{returnvoid(),t;})({});std::coutT{returnvoid(),std::move(t);})({});std::cout当由gcc-4.7.1编译时输出(link):moveconstructorcopyconstructormoveconstructor为什么逗号操作符会有这种效果?标准说:5.18Commaoperator[expr.comma]1-[...]Thetypeandvalueoftheresu

c++ - move 由逗号运算符抑制的构造函数

这个程序:#includestructT{T(){}T(constT&){std::coutT{returnt;})({});std::coutT{returnvoid(),t;})({});std::coutT{returnvoid(),std::move(t);})({});std::cout当由gcc-4.7.1编译时输出(link):moveconstructorcopyconstructormoveconstructor为什么逗号操作符会有这种效果?标准说:5.18Commaoperator[expr.comma]1-[...]Thetypeandvalueoftheresu

c++ - 这里需要 `std::move` 吗?

是std::move以下代码段中是否需要?std::functionmy_std_function;voidcall(std::function&&other_function){my_std_function.swap(std::move(other_function));}据我所知call()接受右值引用..但由于右值引用本身就是一个左值,为了调用swap(std::function&&)我必须使用std::move将其重新转换为右值引用我的推理是正确的还是std::move在这种情况下可以省略(如果可以,为什么?) 最佳答案

c++ - 这里需要 `std::move` 吗?

是std::move以下代码段中是否需要?std::functionmy_std_function;voidcall(std::function&&other_function){my_std_function.swap(std::move(other_function));}据我所知call()接受右值引用..但由于右值引用本身就是一个左值,为了调用swap(std::function&&)我必须使用std::move将其重新转换为右值引用我的推理是正确的还是std::move在这种情况下可以省略(如果可以,为什么?) 最佳答案

C++ 11 : is a defaulted copy constructor user declared?

我猜是这样,但我正在寻找C++11语言律师来确认我的印象。下面的课是真的吗structX{X(){}X(Xconst&)=default;};不会自动启用移动,即获取X(X&&)和operator=(X&&),因为它的复制构造函数是“用户声明的”,即使它看起来等同于structX{};这将获得X(Xconst&)和X(X&&)等,在使用时隐式声明和(平凡)定义。 最佳答案 来自标准:8.4.2Explicitly-defaultedfunctions[dcl.fct.def.default]4-[...]Aspecialmember

C++ 11 : is a defaulted copy constructor user declared?

我猜是这样,但我正在寻找C++11语言律师来确认我的印象。下面的课是真的吗structX{X(){}X(Xconst&)=default;};不会自动启用移动,即获取X(X&&)和operator=(X&&),因为它的复制构造函数是“用户声明的”,即使它看起来等同于structX{};这将获得X(Xconst&)和X(X&&)等,在使用时隐式声明和(平凡)定义。 最佳答案 来自标准:8.4.2Explicitly-defaultedfunctions[dcl.fct.def.default]4-[...]Aspecialmember