2014-10-11 48 views
0

我目前正在用C++编程一个国际象棋程序。用于搜索的算法是alphaBeta,这就是为什么我在通过它们并评估它们之前对它们进行排序。我的主要课程是类位置,它可以完成所有搜索,并且还包含一个用于比较两个Move的函数。通过绑定返回的函子的C++类型

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); 

} 

我们在currentSearch的招式进行排序,我总是在的Alpha-Beta

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

所以这产生函子与bind(mem_fn(&Position::compare),this,_1,_2)); 做几次拨打一个搜索内生成相同的仿函数。

我希望有一个类位置的成员初始化为bind(mem_fn(&Position::compare),this,_1,_2));但是该成员必须拥有什么类型,或者设计此类型的正确方法是什么?

回答

3

我猜你正在使用C++ 11,因为std::bind在以前的版本中不可用。

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

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

显然标准委员会并不想硬编码的实施细则为标准。

所以,如果你真的需要存储函数的地方,最好的选择是写你自己的函子类,而不是使用lambda或std::bind。第二个最佳选择是使用std::function。它可以存储任何类似具有兼容签名的函数,但是会带来运行时间间接的开销,这可能不是您想要的。

或者您可以复制并粘贴相同的行,或者如果函数在同一函数内重用,则使用auto

顺便说一下,当您使用std::bind时,不需要mem_fun

0

从我使用boost.bind的经验(std绑定是基于此),提前捕获结果没有太多意义。如上所述,这种类型很奇怪,只在编译时才存在。

这就是要点。绑定背后的大部分工作都是在编译时完成的。类的初始化很简单,就像使用bind时调用的函数一样。

根据我的经验,绑定的魔力是在编译时使用模板完成的。