c++ - std::bind 不工作

标签 c++ c++11 bind

我无法让 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/

相关文章:

c++ - 将 boost 绑定(bind)处理程序作为参数传递给 asio 绑定(bind)处理程序

C++ 链接库的标志在哪里记录?

c++ - 模板中的无名枚举

c++ - "no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’“

c++ - 字符串流的部分拷贝

javascript - 如何向同一个 onclick 事件添加额外的功能?

c++ - 如何将值应用于结构内的字符串变量?

c++ - 定时器的使用

c++11 具有 avr __progmem__ 的广义属性

php - 如何将 Json 绑定(bind)到 Symfony2 中的实体