草庐IT

nothrow_move_constructible

全部标签

c++ - 为什么模板参数上的 is_copy_constructible 静态断言失败?

我试图对模板参数进行静态断言,以检查/强制Type可复制构造。但是静态断言失败。我不明白为什么,也找不到任何文档为什么它会在静态评估中失败。实例化的类是可复制构造的,但是它使用了我认为被称为奇怪的重复模板参数模式的东西。完整的测试代码如下:#include#includeusingnamespacestd;templateclassFunContainer{//static_assert(is_copy_constructible::value,"Typemustbecopyconstructible!");//::value//::value;};};classFun:publicF

C++ 无法使用通用引用 move unique_ptr

考虑这段代码:templateTmov(T&&t){returnstd::move(t);}intmain(){std::unique_ptra=std::unique_ptr(newint());std::unique_ptrb=mov(a);}mov函数应该简单地接受一个通用引用并按值返回它,但是通过move而不是复制它。因此,调用此方法时不应涉及复制。因此,使用只能move的unique_ptr调用这样的函数应该没问题。但是,此代码无法编译:我收到错误:test.cpp:24:34:error:useofdeletedfunction‘std::unique_ptr::uniqu

c++ - move 类数据成员 (C++)

我想知道我这样做是否正确。我有一个包含一些数据的类:classFoo{//...Typea_;Typeb_;Typec_;};还有一个做其他事情的不同类,但使用classFoo构造。所以,我想像这样声明一个ctor:classBar{Typea_;Typeb_;Typec_;AnotherTypeA_;AnotherTypeB_;//...public:typedefstd::tupleTuple;Bar(constTuple&);Bar(Tuple&&);};我现在需要创建一个Foo方法,它将返回Bar需要的数据成员元组,我可以将其传递给Bar的导演。我还为Tuple创建了一个右值引

c++ - 如何将对象 move 到未初始化的内存中?

给定一个已分配但未初始化的内存位置,我如何将一些对象move到该位置(破坏原始位置),而不构造可能昂贵的中间对象? 最佳答案 您可以使用placementnew在内存中move构造它:void*memory=get_some_memory();Thing*new_thing=new(memory)Thing(std::move(old_thing));如果它有一个非平凡的析构函数,那么你需要在完成后显式地销毁它:new_thing->~Thing(); 关于c++-如何将对象move到未

c++ - 为什么不能 move 这些变量?

C++move语义中似乎错失了很多机会。我想了解这些背后的基本原理,以及为什么标准在定义以下情况下何时应move变量时没有更积极地定义:stringf(){strings;returns+"";}这调用了operator+(conststring&,constchar*),而不是operator+(string&&,constchar*),我相信是因为s是一个左值。难道标准不能说,在函数中最后一次使用局部变量时,该变量被认为是可move的吗?我觉得有点类似的例子:structA{A(string&&);};stringg(){strings;returns;//sismoved}Ah(

c++ - 如何改进 std::vector 参数传递( move 语义?)

我似乎无法完全理解move语义:我想从外部函数填充std::vector(类的成员)。目前,我有类似的东西:voidfillVector(MyClass&myclass){std::vectorvec;/*Fillingvec*///...myclass.setVector(vec);}classMyClass{public:setVector(conststd::vector&v){v_=v;}private:std::vectorv_;};intmain(){MyClassmyclass;fillVector(myclass);/*Usemyclass.v_somehow*/.}我

c++ - 为什么是 "move semantics"而不是简单的 memcpy?

给定以下代码:typenamestd::aligned_storage::typestorage_t;//thismovesthebackofsrctothebackofdst:voidpush_popped(std::list&dstLst,std::list&srcLst){auto&src=srcLst.back();dstLst.push_back(storage_t());auto&dst=dstLst.back();std::memcpy(&dst,&src,sizeof(T));srcLst.pop_back();}我知道这种方法通常不正确的3个原因(即使它避免调用src

c++ - 最佳C++ move 构造函数实现实践

我想了解move构造函数的实现。我们都知道如果我们需要在C++类中管理资源,我们需要实现五法则(C++编程)。微软给了我们一个例子:https://msdn.microsoft.com/en-us/library/dd293665.aspx这是一个更好的方法,它使用复制交换来避免代码重复:Dynamicallyallocatinganarrayofobjects//C++11A(A&&src)noexcept:mSize(0),mArray(NULL){//Canwewritesrc.swap(*this);//or(*this).swap(src);(*this)=std::move

c++ - sort() 是否自动使用 move 语义?

isocpp.org指出:move-basedstd::sort()andstd::set::insert()havebeenmeasuredtobe15timesfasterthancopybasedversions[...]ifyourtypehasamoveoperation,yougaintheperformancebenefitsautomaticallyfromthestandardalgorithms.这是否意味着如果您在没有move构造函数或move赋值运算符的用户定义类型上调用sort(),那么就没有使用move语义?换句话说,要获得C++11性能改进的诸多好处,您应

c++ - 同一对象的双 move 是从左向右复制吗?

我只是c++11中move操作的初学者,所以玩它。但是发现了一些我无法理解的东西。#includeusingnamespacestd;classA{public:A(){coutCORRECT!!coutCORRECT!!b=std::move(a);//idon'tknowmaybesillybutstillletsdoitWHYNOT!!!,couldbejustmistake??coutWRONG!!coutWRONG!!}我原以为b=std::move(a)操作会有所不同,因为我第二次在对象a上应用move,但它正在向左复制侧面对象b到右侧对象a,这部分我不明白。或者我在编程中