2011-07-30 36 views
1

可能嗎?指令bool b = (boost::bind(func, 1) == boost::bind(func, 1))不能編譯,因爲它「無法從'boost :: _ bi :: bind_t'轉換爲'bool'」。 (中func簽名是void func(int)比較Boost.Bind返回的對象?

+0

你爲什麼要比較函數指針或類似的東西? –

+0

...因爲我需要......我應該把它當作「不,這不可能」嗎? –

+0

不,我不知道,但我不認爲boost :: bind的返回類型有operator == defined –

回答

1

不知道這是「oficially支持的功能」,但bind_t似乎提供了function_equal方法:http://www.boost.org/doc/libs/1_47_0/boost/bind/bind.hpp

+0

嘿,這似乎很有前途,非常感謝。我明天會測試這個;我現在很累。 :) –

+0

謝謝你,巴勃羅,它的工作原理。作爲替代,你也可以使用比較成員函數:'boost :: bind(func,1).compare(boost :: bind(func,1))''。 –

3

Boost.Bind重載返回嵌套關係運算符綁定表達式。因此,在您的代碼boost::bind(func, 1) == boost::bind(func, 1)中返回一個空(因爲在綁定表達式中沒有佔位符)仿函數,當調用時返回func(1) == func(1)。這是謂詞一個方便的功能,除其他用途:

typeded std::pair<T, U> pair_type; 
// find pair where the first element is equal to 3 
std::find_if(begin, end, boost::bind(&pair_type::first, _1) == 3); 

此外,返回的對象是無法轉換爲bool,這就是爲什麼它不會編譯(不考慮這個問題,它並沒有做什麼你要)。

你想要做的不是Boost.Bind接口的一部分。 Tt不會是一個非常有用的功能,在非常普遍的情況下是undecidable