我无法让 std::bind
以与 boost::bind
相同的方式工作。要么是我没有正确使用它,要么是我的编译器 (GCC 4.4.5) 还没有正确实现它。
我有两个功能:
void f(int x, int y)
{
cout << x << " | " << y << endl;
}
template <class UnaryFunction>
void g(UnaryFunction func)
{
func(100);
}
我使用 bind 来调用 f
作为 g 中的一元函数:
g(std::bind(f, 10, std::placeholders::_1));
这会导致编译器错误:
error: no match for call to ‘(std::_Bind<void (*(int, std::_Placeholder<1>))(int, int)>) (int)’
... 接着是一页左右的模板编译器呕吐物。
如果我使用 boost::bind
,例如:
g(boost::bind(f, 10, _1));
...它工作正常。 std::bind
的语义有何不同,还是编译器问题?
最佳答案
看起来这只是你的编译器版本,gcc 4.5.1 ( via ideone.com ) 和 4.6.0 编译正确。
关于c++ - std::bind 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5690349/