// 如果參數(shù)為const int&類型,就會(huì)掛掉。據(jù)說是編譯器實(shí)現(xiàn)的時(shí)候忽略了?
// 具體分析錯(cuò)誤應(yīng)該是這樣: std::ptr_fun會(huì)構(gòu)造出一個(gè)派生于binary_function的對(duì)象,
// 傳遞給他的模板參數(shù)就是函數(shù)參數(shù)類型,如果傳遞&類型,會(huì)導(dǎo)致調(diào)用真是函數(shù)時(shí)候
// argument_type&變成argument_type&&引發(fā)編譯錯(cuò)誤,除非能在std::prt_fun中推導(dǎo)出
// Val&參數(shù)類型中的Val類型作為模板參數(shù)傳遞下去
bool Cmp(const int& iLeft, const int& iRight)
{
return true;
}
// std::binary_functiond在傳遞函數(shù)參數(shù)的時(shí)候已經(jīng)分別生命了const TVal& 和 TVal&兩個(gè)版本,
// 所以在實(shí)例化的時(shí)候不能傳遞const TVal&上去,會(huì)造成編譯錯(cuò)誤
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的區(qū)別
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));
}