調用STL:調用bind2nd與ptr_fun的 「常量T&」 類型
std::count_if(vec.begin(), vec.end(), std::bind2nd(std::ptr_fun(foo), 17))
正常工作與
bool foo(int, int),
,但我不能使它與工作
bool foo(const int &, const int &)
有沒有辦法做到這一點,或者我必須編寫自己的適配器功能?
調用STL:調用bind2nd與ptr_fun的 「常量T&」 類型
std::count_if(vec.begin(), vec.end(), std::bind2nd(std::ptr_fun(foo), 17))
正常工作與
bool foo(int, int),
,但我不能使它與工作
bool foo(const int &, const int &)
有沒有辦法做到這一點,或者我必須編寫自己的適配器功能?
第二個參數是一個數,並且不能被轉換爲const int &
。
您可以使用boost::bind
這樣的伎倆:
std::count_if (vec.begin(), vec.end(), boost::bind (foo, _1, 17));
編輯:
由於我的第一反應,是的,你不能使用,而不是數字的變量。我認爲問題是bind2nd
和ptr_fun
沒有被正確定義爲解除引用這種類型的情況下,它是一個引用的情況下,當ptr_fun
建立內部Operation
對象,所以ither隨着增強或寫你自己的函子類。
這沒有什麼區別。錯誤是: '類型名_Operation :: result_type的標準:: binder2nd <_Operation> ::運算符()(類型名_Operation :: first_argument_type&)const的[與_Operation =標準:: pointer_to_binary_function
你是對。看我的編輯。 –
你爲什麼想用const引用反正通過INT的?任何POD類型都可以放在堆棧上,沒有缺點。 – Dark
這不是關於int的。記住更復雜的對象 – Michael