草庐IT

move-constructor

全部标签

C++11 - 单独 move 数组(原始数组、std::array、std::vector)的每个元素?

在C++11中,move语义等等,人们可能想知道实际上可以move什么。这方面的一个例子是数组。是否可以move原始数组的每个元素,intarray1[8];intarray2[8];array1[0]=std::move(array2[0]);std::数组,std::arrayarray1;std::arrayarray2;array1[0]=std::move(array2[0]);和std::vectorsstd::vectorarray1;std::vectorarray2;array1[0]=std::move(array2[0]);个人? 最佳

c++ - 复制/移动构造函数的奇怪行为以及如何返回大对象?

我最近再次尝试使用C++11,在离开一段时间后,在阅读互联网上的许多文章后,我现在完全困惑什么是从工厂函数返回大对象的最有效方法(基本上,数据从数据库分析)。我已经成为unique_ptr的粉丝,但我在几篇文章中读到,由于有了新的移动构造函数,现在完全可以按值返回一个大vector,并且由于这些新的语义,它应该像复制一个指针一样快。为了尝试这个,我写了一个小测试程序,在各种构造函数中输出:#include#includeusingnamespacestd;classC{public:C(stringn):_name{n}{cout测试CfooVal(){coutfooUPtr(){co

c++ - 将从函数返回 vector<vector <int>> 调用 C++11 中的任何 move 构造函数

在C++11中将返回一个vector>从函数调用任何move构造函数?还是下面的代码只是制作所有vector及其元素的另一个拷贝?vector>Func(){vector>vec;//vecisfilledherereturnvec;}对于简单类型的STL容器,当使用move构造函数或将它们作为函数值返回时进行复制时,是否有一个通用的发现? 最佳答案 return语句在标准中特别涵盖,可自动视为move。所以是的,这将调用move构造函数。这方面的法律条文是C++11,[class.copy]§31+32:31Whencertain

LeetCode --- 2027. Minimum Moves to Convert String 解题报告12

https://h5.weishi.qq.com/weishi/feed/7OLnHCrBU1Rx4Avoshttps://m.weishi.qq.com/vise/share/index.html?id=7OLnHCrBU1Rx4Avoshttps://m.weishi.qq.com/vise/share/index.html?id=7OLnHCrBU1Rx4Avos&js=oszhttps://h5.weishi.qq.com/weishi/feed/7OLnHyfsP1Rx4AsKMhttps://m.weishi.qq.com/vise/share/index.html?id=7OLn

c++ - "Moving out"对象的内部表示。好不好?

假设我有一个类,它的内部数据表示是一个std::string:classmy_type{std::stringm_value;...};如果我可以“移出”my_type的内部表示,会不会很好?这种能力将以如下方式完成:classmy_type{std::stringm_value;public:operatorstd::string()&&{//NOTE:^^refqualifierforr-valuereturnstd::move(m_value);//Explicitlydostd::moveisusedbecauseref-qualifiersdon'tapply//todata

c++ - 使用 unique_ptr 成员编写移动构造函数的正确方法(崩溃)

以下代码在VisualStudio2013下会崩溃我想知道为什么:在这种情况下编写移动构造函数的正确方法是什么?删除移动构造函数解决了这个问题。是VC++的错误还是这段代码有误?移动构造函数的默认定义有何不同,这使得这段代码不会崩溃,而我自己的定义会崩溃?#include#includeclassA{};classFoo{public:Foo(std::unique_ptrref):mRef(std::move(ref)){}Foo(Foo&&other):mRef(std::move(other.mRef)){}Foo(constFoo&other){}Foo&operator=(c

c++ - 在继承的构造函数中仅 move 类参数

以下代码无法编译GCC6.1,但适用于Clang3.8.0和VisualStudio2015:#includeclassbase{public:base(std::unique_ptr){}};classderived:publicbase{public:usingbase::base;};intmain(){deriveddf(std::make_unique());}出现以下错误:main.cpp:Inconstructor'derived::derived(std::unique_ptr)':main.cpp:10:17:error:useofdeletedfunction'st

c++ - 为派生类 move 构造函数

我有2个类:templateclassbase{Tt;public:base(base&&b):t(std::move(b.t)){}};templateclassderived:protectedbase{T2t2;public:derived(derived&&d):base(std::move(d)),t2(std::move(d.t2)){}};我在derivedmove-constructor中move整个d对象来初始化base部分和d变得无效,但我仍然需要它来使用它作为t2初始化的一部分有可能做这样的事情吗? 最佳答案

c++ - std::string not nothrow move assignable or comparable?

我在研究type_traits时,发现了std::string的这个奇怪属性:$cata.cpp#include#includestatic_assert(std::is_nothrow_move_assignable::value,"???");static_assert(noexcept(std::declval()==std::declval()),"???");$g++-std=c++14a.cppa.cpp:4:1:error:staticassertionfailed:???static_assert(std::is_nothrow_move_assignable::val

c++ - 在此上下文中的完美转发和 std::move 行为

我是C++新手,我想了解完美转发如何与std::move结合使用.我定义了一个std::vectorqueue()我想使用模板函数填充fillWithData.由于我花了一些时间研究完美转发,所以我首先要检查我是否理解正确,其次要弄清楚move是什么。在此上下文中的行为。fillWithData是一个可变参数模板函数,感谢forward,能够通过折叠规则将参数视为左值或右值。(Q1-是否正确?)templatestaticvoidfillWithData(Container&oDataContainer,Args&&...args)//universalreference{typede