简体   繁体   English

绑定返回的C ++函子的类型

[英]C++ Type of a functor returned by bind

I'm currently programming a chess program in C++. 我目前正在用C ++编写国际象棋程序。 The algorithm for searching is alphaBeta, that's why I sort the moves before going through them and evaluating them. 搜索算法是alphaBeta,这就是为什么我在对移动进行评估和评估之前对其进行排序的原因。 My main class is the class Position, that does all the searching and also contains a function to compare two Moves. 我的主要课程是Position类,它可以进行所有搜索,还包含一个比较两个Moves的函数。

class Position{

    private:
    //This vector holds the moves of the current line, to be evaluated

    vector<Move> currentSearch(4000);

    // This function uses internal fields of class Position to determine, whether move1 or move2 should be searched first

    bool compare(const Move& move1, const Move& move2);

    int alphaBeta(int ply, int depth, int alpha, int beta);

}

Now to sort the moves in currentSearch, I always call in alphaBeta 现在要对currentSearch中的动作进行排序,我总是调用alphaBeta

sort(currentSearch.begin(), currentSearch.end(), bind(mem_fn(&Position::compare),this,_1,_2));

So this generating of a functor with bind(mem_fn(&Position::compare),this,_1,_2)); 因此使用bind(mem_fn(&Position::compare),this,_1,_2));生成函子 is done several times inside one search to generate the same functor. 在一次搜索中完成了几次,以生成相同的仿函数。

I would like to have a member of class Position that is initialized to bind(mem_fn(&Position::compare),this,_1,_2)); 我想拥有一个初始化为bind(mem_fn(&Position::compare),this,_1,_2));类的成员bind(mem_fn(&Position::compare),this,_1,_2)); But what type that member must have, or what is the correct way to design this? 但是该成员必须具有哪种类型,或者设计该成员的正确方法是什么?

I guess you are using c++11 because std::bind is not available in previous editions. 我猜您正在使用c ++ 11,因为std::bind在以前的版本中不可用。

From http://en.cppreference.com/w/cpp/utility/functional/bind : http://en.cppreference.com/w/cpp/utility/functional/bind

template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );

Obviously the standard committee does not want to hard code the implementation details into the standard. 显然,标准委员会不想将实施细节硬编码到标准中。

So if you really need to store the functor somewhere, the best choice is write your own functor class instead of using lambda or std::bind . 因此,如果您真的需要将函子存储在某个地方,最好的选择是编写自己的函子类,而不要使用lambda或std::bind The second best choice is to use a std::function . 第二个最佳选择是使用std::function It can store any function like things with compatible signature, but with the overhead of runtime indirection, which you probably do not want here. 它可以存储任何具有兼容签名的功能,例如具有兼容签名的功能,但带有运行时间接调用的开销,您在这里可能不希望这样做。

Or you can just copy and paste the same lines, or use auto if the functor is reused inside the same function. 或者,您可以仅复制和粘贴相同的行,如果函子在同一函数中重复使用,则可以使用auto

By the way, there is no need for mem_fun when you use std::bind . 顺便说一句,当您使用std::bind时,不需要mem_fun

From my experience with boost.bind (which std bind is based on), there's not much point in capturing the results ahead of time. 从我对boost.bind(基于std bind的基础)的经验来看,提前捕获结果没有多大意义。 As noted above the type is rather odd and only exists during compile time. 如上所述,该类型相当奇怪,仅在编译时存在。

And that's part of the point. 这就是重点。 Most of the work behind bind is done at compile time. bind的大部分工作都是在编译时完成的。 The initialization of the class is trivial as is the function calling when using bind. 与使用bind时调用函数一样,类的初始化很简单。

From my experience, the magic of bind is done with templates at compile time. 根据我的经验,绑定的魔力是在编译时使用模板完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM