nothrow_move_constructible
全部标签 这是Explicitref-qualifiedconversionoperatortemplatesinaction的后续事件.我已经尝试了许多不同的选项,我在这里给出了一些结果,试图看看最终是否有任何解决方案。假设一个类(例如any)需要以一种方便、安全(毫无意外)的方式提供对任何可能类型的转换,同时保留move语义。我能想到四种不同的方法。structA{//explicitconversionoperators(nice,safe?)templateexplicitoperatorT&&()&&;templateexplicitoperatorT&()&;templateexpl
此代码使用gcc4.8.2(-std=c++11)编译失败,但使用clang3.4(trunk)(-std=c++11)编译:#include#includestructX{X&operator=(X&&)noexcept=default;//addingnoexceptthisleadstoanerroringcc,butworksinclang://function‘X&X::operator=(X&&)’defaultedonitsfirst//declarationwithanexception-specificationthatdiffersfromthe//implicit
我正在查看一些代码,我看到以下函数:templatestaticreturn_tmake_return(Args&&...args){//usingstd::forwardwillpreservelvalueargsassuch,butthepointofthisfunction//istomakeareturn,wherethe99.9+%caseismovingalocal(lvalue)intothereturnpack.//Thusitforcesamove,whichwillmove`T&`args(but_not_`constT&`args)intothe//returnp
这是std::is_copy_constructible(1)和std::is_trivially_copy_constructible文档的摘录(2)关于cppreference.com:1)CheckswhetheratypeisCopyConstructible,i.e.hasanaccessibleexplicitorimplicitcopyconstructor.Iftherequirementismet,amemberconstantvalueequaltrueisprovided,otherwisevalueisfalse.2)Sameas(1),butthecopyco
以下代码无法使用VisualStudio2013编译:#includestructX{X()=default;X(constX&)=delete;X&operator=(constX&)=delete;X(X&&)=delete;X&operator=(X&&)=delete;~X()=default;};voidfoo(){std::vectorv;std::vectorw;w=std::move(v);}错误信息说errorC2280:'X::X(X&&)':attemptingtoreferenceadeletedfunction这对我来说毫无意义。您应该不需要X的move构造函
对具有引用成员变量的类进行复制分配是禁忌,因为您无法重新分配引用。但是move分配呢?我尝试简单地move,但是当我只想move引用本身时,这当然会破坏源对象:classC{public:C(X&x):x_(x){}C(C&&other):x_(std::move(other.x_)){}C&operator=(C&&other){x_=std::move(other.x_);}private:X&x_;};Xy;Cc1(y);Xz;Cc2(z);c2=c1;//destroysyaswellasz我不应该只实现move分配并坚持move构造吗?这使得swap(C&,C&)难以实现。
new运算符(或对于POD,malloc/calloc)在分配大块内存时支持一种简单而有效的失败形式。假设我们有这个:constsize_tsz=GetPotentiallyLargeBufferSize();//1M-1000MT*p=new(nothrow)T[sz];if(!p){returnsorry_not_enough_mem_would_you_like_to_try_again;}...std::containers是否有任何这样的构造,或者我总是必须处理std::vector和friend的(预期的!!)异常?可能有一种方法可以编写一个自定义分配器来预分配内存,然后将
看完thisrecentquestion@Mehrdad关于应该使哪些类不可move因此不可复制,我开始想知道是否有一个类的用例可以复制但不能move.从技术上讲,这是可能的:structS{S(){}S(Sconst&s){}S(S&&)=delete;};Sfoo(){Ss1;Ss2(s1);//OK(copyable)returns1;//ERROR!(non-movable)}虽然S有一个复制构造函数,但它显然没有模拟CopyConstructible的概念,因为那又是对MoveConstructible的改进概念,它需要存在(未删除的)move构造函数(参见第17.6.3.1
我使用VS11并使用以下内容:classContextWrapper{public:ContextWrapper(){}//itshouldbedefaultedI*guess*inordertohaveautomaticmoveconstructor?//nosupportinVS11forthatnowContext*GetContext(){returnthis->context.get();}voidSetContext(std::unique_ptrcontext){this->context=std::move(context);}//ContextWrapper(Cont
以下程序在使用GCC4.7和clang3.2编译时,会产生“1”作为输出。#includestructfoo{templatefoo(T){static_assert(notstd::is_same(),"nointsplease");}};#includeintmain(){std::cout();}这令人困惑。foo显然不能从int构造!如果我将main更改为以下内容,两个编译器都会因为静态断言失败而拒绝它:intmain(){foo(0);}为什么两个编译器都说它是可构造的? 最佳答案 这是标准必须说的(§20.9.5/6),