2014-02-15 41 views
3

此代碼是開發的,最初使用boost 1.48。由於升級到1.52,它拒絕編譯。綁定重載函數

boost::signals2::signal<void (uint32_t)> foo; 
boost::shared_ptr<boost::circular_buffer<uint32_t>> _foo_buffer = new boost::circular_buffer<uint32_t>(32); 
_foo.connect(boost::bind(&boost::circular_buffer<uint32_t>::push_front, _foo_buffer, _1)); 

的目標是具有foo(42)地方42在環形緩衝器上的前部。

我的編譯錯誤是No matching function for call to 'bind'與一堆Candidate template ignored: couldn't infer template argument 'R'和類似的模板錯誤。

我懷疑的問題是,我使用升壓版本之間,爲push_front定義從

void push_front(const_reference item = value_type()); 

改爲三個定義

void push_front(param_value_type); 
void push_front(rvalue_type); 
void push_front(); 

和它混淆我的編譯器。

我真的很感謝與新的boost庫一起工作的連接表達式的語法。由於

回答

1

讓我們說你有兩個功能

void foo(int) {} 
void foo(double*) {} 

,那麼你不能只寫&foo,因爲這將是不明確的。

但你可以使用static_cast建立上下文,其中只有一名候選人是可能的,正是如此:

static_cast<void(*)(int)>(&foo) 

它給你的第一個地址,並同上,對第二個。


中說,它看起來像你可以使用C++ lambda函數,而不是一個bind(一個C++編譯器03或者定義自己的functot)簡化事情。

+0

靜態轉換方法不適用於我,但它可能是一個語法錯誤。 ''static_cast :: *)(uint32_t)>(&boost :: circular_buffer :: push_front)'只是產生了與原始類似的錯誤。我現在不在我的電腦,所以我不能引用他們。 但是,C++ lambda對我來說非常合適,所以它不再是一個問題。謝謝。 – JamesL