如何绑定到vector<>::at?

How to bind to vector<>::at?

本文关键字:lt gt at vector 何绑定 绑定      更新时间:2023-10-16

在Visual C 2013中,以下代码给我一个"模棱两可的呼叫"编译错误:

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)'

我该如何正确执行?

V::at的返回类型是 V::reference

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

不必说,这是一个可憎的。

如果您不需要使用bind,则可以使用std::function

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