使用 std::bind 时,我能够通过传递指针或迭代器而不是对象本身来绑定 VS2010 中的数据成员。但是,它似乎不再适用于 VS2012:
#include <vector>
#include <utility>
#include <iostream>
#include <functional>
using namespace std;
int main()
{
vector<pair<string, int>> v;
v.push_back(make_pair("abc", 10));
auto f = bind(&pair<string, int>::second, v.begin());
int res = f();
cout << res << endl;
return 0;
}
GCC 也可以很好地编译和运行这段代码,但是 VS2012 给了我一个错误:
error C2440: 'initializing' : cannot convert from 'std::_Do_call_ret<_Forced,_Ret,_Funx,_Btuple,_Ftuple>::type' to 'int'
1> with
1> [
1> _Forced=false,
1> _Ret=void,
1> _Funx=std::_Pmd_wrap<int std::pair<std::string,int>::* ,int,std::pair<std::string,int>>,
1> _Btuple=std::tuple<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::pair<std::string,int>>>>>,
1> _Ftuple=std::tuple<>
1> ]
1> Expressions of type void cannot be converted to other types
请注意,如果我传递一个 std::pair 实例而不是迭代器或指向它的指针,那么 VS2012 很高兴。
这里有什么问题?