// 如果參數為const int&類型,就會掛掉。據說是編譯器實現的時候忽略了?
// 具體分析錯誤應該是這樣: std::ptr_fun會構造出一個派生于binary_function的對象,
// 傳遞給他的模板參數就是函數參數類型,如果傳遞&類型,會導致調用真是函數時候
// argument_type&變成argument_type&&引發編譯錯誤,除非能在std::prt_fun中推導出
// Val&參數類型中的Val類型作為模板參數傳遞下去
bool Cmp(const int& iLeft, const int& iRight)
{
return true;
}
// std::binary_functiond在傳遞函數參數的時候已經分別生命了const TVal& 和 TVal&兩個版本,
// 所以在實例化的時候不能傳遞const TVal&上去,會造成編譯錯誤
class Functor_Cmp : public std::binary_function<int, int, bool>
{
public:
bool operator () (const int& iLeft, const int& iRight) const
{
return true;
}
};
void Test_Bind2end()
{
vector<int> vInt(9);
// 注意functor 和function ptr的區別
std::count_if(vInt.begin(), vInt.end(), std::bind2nd(std::ptr_fun(&Cmp), 1));
std::count_if(vInt.begin(), vInt.end(), std::bind2nd(Functor_Cmp(), 1));
}