2014-01-24 28 views
3

在Visual C++ 2013下面的代碼給了我一個「曖昧電話」編譯錯誤:如何綁定到vector <> :: at?

typedef vector<int> V; 
V v; 
auto b1 = bind(&V::at, &v); 

現在我已經搜索了一圈,發現我應該鑄造我要簽名。所以我這樣做:現在

auto b2 = bind(static_cast<int(V::*)(V::size_type)>(&V::at), &v); 

,錯誤的是:

'static_cast' : cannot convert from 'overloaded-function' to 'int (__thiscall std::vector<_Ty>::*)(unsigned int)' 

我該怎麼辦呢正確?

回答

5

V::at返回類型爲V::reference

auto b = std::bind(static_cast<V::reference (V::*)(V::size_type)>(&V::at), v); 

不用說,這是一個令人深惡痛絕。

+0

是的,但它的邏輯。我只是沒有看到它。 –

1

如果你不使用需要bind,你也許可以使用std::function

std::function<int&(int)> b1 = [&v](int index){return v.at(index);} 
+0

不,我不能,因爲我使用它從C++/CLI,其中不允許本地lambdas。 –

相關問題