c++ - boost::bind 成员函数 - 部分应用程序链接

标签 c++ boost bind partial-application

我正在尝试使用 boost::bind 将 curried 函数链接在一起,并遇到我无法解决的编译器错误。我能做的最简单的编译失败的例子:

#include <iostream>
#include <boost/bind.hpp>

class A
{
public:
    template <typename F>
    void g(F fn, char c)
    {
        fn(c);
    }

    void h(char c)
    {
        std::cout << c << std::endl;
    }

    template <typename F>
    void f(F fn, char c)
    {
        boost::bind(&A::g<F>, this, fn, ::_1)(c);
    }
};

int main(int argc, char** argv)
{
    char c = 'a';
    A a;
    a.f(boost::bind(&A::h, &a, ::_1), c);
}

失败并出现此错误:

In file included from /usr/include/boost/bind.hpp:22,
                 from test8.cpp:2:
/usr/include/boost/bind/bind.hpp: In member function ‘void boost::_bi::list3<A1, A2, A3>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = boost::_mfi::mf2<void, A, boost::_bi::bind_t<void, boost::_mfi::mf1<void, A, char>, boost::_bi::list2<boost::_bi::value<A*>, boost::arg<1> > >, char>, A = boost::_bi::list1<char&>, A1 = boost::_bi::value<A*>, A2 = boost::_bi::bind_t<void, boost::_mfi::mf1<void, A, char>, boost::_bi::list2<boost::_bi::value<A*>, boost::arg<1> > >, A3 = boost::arg<1>]’:
/usr/include/boost/bind/bind_template.hpp:32:   instantiated from ‘typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = char, R = void, F = boost::_mfi::mf2<void, A, boost::_bi::bind_t<void, boost::_mfi::mf1<void, A, char>, boost::_bi::list2<boost::_bi::value<A*>, boost::arg<1> > >, char>, L = boost::_bi::list3<boost::_bi::value<A*>, boost::_bi::bind_t<void, boost::_mfi::mf1<void, A, char>, boost::_bi::list2<boost::_bi::value<A*>, boost::arg<1> > >, boost::arg<1> >]’
test8.cpp:21:   instantiated from ‘void A::f(F, char) [with F = boost::_bi::bind_t<void, boost::_mfi::mf1<void, A, char>, boost::_bi::list2<boost::_bi::value<A*>, boost::arg<1> > >]’
test8.cpp:29:   instantiated from here
/usr/include/boost/bind/bind.hpp:385: error: invalid use of void expression

这是在使用

$ g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3

最佳答案

在这种情况下,您需要使用 boost::protect(),当您将一个绑定(bind)表达式传递给另一个绑定(bind)调用,但不希望在评估参数时调用嵌套的表达式到外部绑定(bind)函数,而是希望嵌套的绑定(bind)表达式按原样作为参数传递给外部绑定(bind)函数。 这将是:

boost::bind(&A::g<F>, this, boost::protect(fn), ::_1)(c);

关于c++ - boost::bind 成员函数 - 部分应用程序链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12689277/

相关文章:

c++ - 使用 std::transform 和 tr1::bind 转换 std::complex 的 vector

c++ - Boost.Test - 问题覆盖主要

c++ - 如何创建 shared_ptr 以传递给采用 void * 的函数

class - Vue中一个条件绑定(bind)多个类

c++ - 来自 glGetError() 的 INVALID_ENUM 错误。 glGetError 与 glu、glew、glfw?

c++ - Boost 是否因为不像 Boost 那样有罪?

python 2.7/Linux : socket library binding type error

c++ - 无法使用 native 消息接收 10 字节的消息

c++ - 使用 SPI_SETMOUSESPEED 不改变鼠标速度

c++ - 应用程序 VS Express 2012 c++ 的设置图标