草庐IT

move_if_noexcept

全部标签

c++ - 为什么使用单个赋值运算符处理复制和 move 赋值效率不高?

这是C++Primer第5版中的一个练习:Exercise13.53:Asamatteroflow-levelefficiency,theHasPtrassignmentoperatorisnotideal.Explainwhy.Implementacopy-assignmentandmove-assignmentoperatorforHasPtrandcomparetheoperationsexecutedinyournewmove-assignmentoperatorversusthecopy-and-swapversion.(P.544)文件hasptr.h://!aclassh

c++ - enable_if 添加具有默认参数的函数参数?

我无法理解呈现的第二种情况here.它说:•Scenario2:Addingafunctionparameterthathasadefaultargument:templateyour_return_type_if_presentyourfunction(args,enable_if_t=BAR){//...}Scenario2leavestheparameterunnamed.Youcouldsay::typeDummy=BAR,butthenameDummyisirrelevant,andgivingitanameislikelytotriggeranunreferencedpar

c++ - std::move 的未定义行为

来自movepage的cppreferenceUnlessotherwisespecified,allstandardlibraryobjectsthathavebeenmovedfromareplacedinavalidbutunspecifiedstate.Thatis,onlythefunctionswithoutpreconditions,suchastheassignmentoperator,canbesafelyusedontheobjectafteritwasmovedfrom因此,从同一页面上的示例来看,下面的代码被认为是未定义的行为vectorv_string;str

c++ - 使用带有匿名类型参数的 std::enable_if

我尝试将std::enable_if与未使用和未命名的类型参数一起使用,以免扭曲return类型。但是,以下代码无法编译。#includetemplate::value>>Tfoo(){std::cout::value>>Tfoo(){std::cout();foo();}编译器说:7:3:error:redefinitionof'templateTfoo()'4:3:note:'templateTfoo()'previouslydeclaredhereInfunction'intmain()':11:12:error:nomatchingfunctionforcallto'foo()

c++ - 为什么从初始化列表中初始化 vector 时不使用 move 构造(通过隐式构造函数)

为了演示move语义,我编写了以下示例代码,其中包含来自int的隐式构造函数。structC{inti_=0;C(){}C(inti):i_(i){}C(constC&other):i_(other.i_){std::cout和autovec2=std::vector{1,2,3,4,5};cout有输出Acopyconstructionwasmade.1Acopyconstructionwasmade.2Acopyconstructionwasmade.3Acopyconstructionwasmade.4Acopyconstructionwasmade.5reversingAmov

c++ - 是否值得添加一个支持 move 的二传手?

这篇文章有点啰嗦,所以在开始之前我想弄清楚我要问的是什么:您是否已将启用move的setter添加到您的代码中并且您是否发现它值得付出努力?我发现的预期行为中有多少可能是特定于编译器的?我在这里关注的是在我设置复杂类型的属性的情况下是否值得添加启用move的setter函数。在这里,我有启用move的Bar和Foo,它有一个可以设置的Bar属性。classBar{public:Bar():_array(1000){}Bar(Barconst&other):_array(other._array){}Bar(Bar&&other):_array(std::move(other._arra

c++ - 重载函数以获取 true_type 或 false_type 参数与使用 if 检查?

与使用一个if语句相比,重载方法/函数以采用true_type或false_type参数有什么好处吗?我看到越来越多的代码使用带有true_type和false_type参数的重载方法。使用if语句的简短示例voidcoutResult(boolmatch){if(match)cout与使用重载函数相比:voidcoutResult(true_type){cout 最佳答案 您的第二个示例代码无法编译,这是编译时重载解析和运行时条件分支之间“选择”哪个不同的症状要执行的代码。“重载函数以获取true_type或false_type参

c++ - move std::thread

尝试制作简单的代码片段:std::threadthreadFoo;std::thread&&threadBar=std::thread(threadFunction);threadFoo=threadBar;//thread&operator=(thread&&other);expectedtobecalled出现错误:useofdeletedfunction'std::thread&std::thread::operator=(conststd::thread&)'我将threadBar明确定义为右值引用,而不是普通引用。为什么不调用预期的运算符(operator)?如何将一个线程m

c++ - 纯虚函数会阻止隐式生成的 move 构造函数吗?

#includestructtest{virtualvoidfoo()noexcept=0;};structtest2:test{voidfoo()noexceptoverridefinal{}};//failsstatic_assert(std::is_move_constructible::value,"testnotmoveconstructible");//succeedsstatic_assert(std::is_move_constructible::value,"test2notmoveconstructible");(Live)根据cppreference.com(据我

c++ - 大 if else 语句

如果有一个大的(大约100多个)ifelse语句,如下所示,并且ifelse条件可能是不规则的(例如,一些依赖于3个变量,一些依赖于4个),有没有办法让它变得更简单?基本上我有一个大约100多行的表,其中a、b、c和d作为列。基于a、b、c和d,我需要执行3种不同类型的功能。该表描述了一组业务规则。uint8a;uint8b;uint16c;uint8d;if(a==1&&b==1&&c==0){functionA();}elseif(a==5&&b==5&&c==2&&d==2){functionB();}elseif(a==1&&(b==36||b==7)&&c==0){funct