سؤال

In Visual C++ 2013 the following code gives me an "ambiguous call" compile error:

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

Now I have searched around and found I should be casting to the signature I want. So I do this:

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

Now, the error is:

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

How can I do it correctly?

هل كانت مفيدة؟

المحلول

The return type of V::at is V::reference:

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

Needless to say, this is an abomination.

نصائح أخرى

If you're not required to use bind you could maybe use std::function :

std::function<int&(int)> b1 = [&v](int index){return v.at(index);}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top