草庐IT

move_if_noexcept

全部标签

c++ - move 左值引用参数是不好的做法吗?

我最初认为move左值引用参数是不好的做法。C++开发人员社区确实普遍认同这一点吗?当我调用一个具有R值引用参数的函数时,很明显我必须期望可以move传递的对象。对于具有L值引用参数的函数,这并不是那么明显(在C++11引入move语义之前,这根本不可能)。但是,我最近采访过的其他一些开发人员不同意应避免move左值引用。是否有强有力的论据反对它?还是我的意见有误?由于我被要求提供一个代码示例,这里有一个(见下文)。这是一个仅用于演示问题的人为示例。很明显,在调用modifyCounter2()之后,再调用getValue()会导致segmentationfault。但是,如果我是ge

c++ - 如何将 enable_if 用于互斥的非成员函数模板?

我正在尝试编写非成员运算符函数模板,例如:#includetemplateclassMyType;templateautooperator==(MyTypeconst&l,MyTypeconst&r)->decltype(std::declval()==std::declval()){/*...*/}但是当我尝试处理l和r的长度不同时:template::type>autooperator==(MyTypeconst&l,MyTypeconst&r)->decltype(std::declval()==std::declval()){/*...*/}templateLu)>::type

c++ - 迭代器或指针的 std::enable_if 或 SFINAE

我想为MyClass编写一个带有参数的构造函数,并且我希望仅当参数是一个pointer或iterator(具有iterator_traits的东西)。如何实现? 最佳答案 遗憾的是,没有标准的方法来检测类是否为Iterator模型。最简单的检查是*it和++it在语法上都是有效的;您可以使用标准SFINAE技术执行此操作:template(),void(),++std::declval(),void())>MyClass(T);考虑到24.2.2:2中的Iterator要求:templatetypenamestd::enable_i

c++ - 返回 tic tac toe 的 minimax 算法中的最佳 Move

我曾尝试编写RusselNorvig关于人工智能的书中给出的tic-tac-toe的minimax算法。它拥有一切,除了将bestMove返回给用户的方法。我正在努力返回bestMove,但无法决定何时选择bestMove。帮忙,有人吗?moveTMiniMax(stateTstate){moveTbestMove;max_move(state,bestMove);returnbestMove;}intmax_move(stateTstate,int&bestMove){intv=-10000;if(GameIsOver(state)){returnEvaluateStaticPosi

c++ - move 构造函数和初始化列表

我想为需要成为boost::unordered_map中的值类型的特定类型实现move构造函数(无复制构造函数).我们称这种类型为Composite.Composite具有以下签名:structBase{Base(..stuff,nodefaultctor):initializationlist{}Base(Base&&other):initializationlist{}}structComposite{Basemember;Composite(..stuff,nodefaultctor):member(...){}Composite(Composite&&other):member

c++ - enable_if 有条件地包含成员函数

我有一个模板类,它的类型是迭代器。我想根据模板参数的iterator_category启用/禁用特定成员函数。特别是,我想启用operator--如果模板参数是双向迭代器。我的尝试是这样的:typenamestd::enable_if::value,MyType&>::typeoperator--(){//doworkreturn*this;}Clang告诉我(大致):error:notypenamed'type'in'std::__1::enable_if';'enable_if'cannotbeusedtodisablethisdeclaration有没有办法完成我正在尝试的事情?

c++ - 如果对象的构造函数是noexcept,placement new(expression)可以抛出吗?

templatestructObj{//PlainOldDataforTusingInternalPod=typenamestd::aligned_storage::value>::type;InternalPodvalue_pod_;templateObj(Args&&...args){//myconstructor//placementnew:constructthevalueinthestaticallyallocatedspacenew(&value_pod_)T(std::forward(args)...);//Normalnew可以在分配失败或构造失败时抛出(如果有其他情况

c++ - std::move 和映射赋值

我对标准如何管理这种情况感到有点困惑:structFoo{Foo&operator=(std::stringxxx){x=std::move(xxx);return*this;}std::stringx;};std::mapbar;std::stringbaz="somestring";bar[baz]=std::move(baz);编译器能否生成代码,以便baz被movebefore它用于初始化和获取对bar中元素的引用(初始化std::stringxxx)?或者这段代码是否安全并且没有未定义的行为? 最佳答案 hell不。表达式

c++ - 在 if-else if 链中使用 Likely()/Unlikely() 预处理器宏

如果我有:#definelikely(x)__builtin_expect((x),1)#defineunlikely(x)__builtin_expect((x),0)if(A)returntrue;elseif(B)returnfalse;...elseif(Z)returntrue;else//thiswillneverreallyhappen!!!!raiseError();returnfalse;我能否像elseif(likely(Z))一样将likely()放在最后一个条件检查周围,以表示在编译器不影响分支预测的情况下最终语句(else)的可能性很小之前的检查?基本上,如果

c++ - 是否可以将 'enable_if' 和 'is_same' 与可变函数模板一起使用?

这两个非可变函数模板编译:templatetypenamestd::enable_if::value,void>::typetestFunction(Ta,Ub){std::couttypenamestd::enable_if::value,void>::typetestFunction(Ta,Ub){std::cout但是,类似的可变参数模板无法编译:templatetypenamestd::enable_if::value,void>::typetestFunction(Ta,U...bs){std::couttypenamestd::enable_if::value,void>: