在嵌套的boost bind中使用模板函数

时间:2011-11-12 01:06:21

标签: c++ templates boost bind

如何模板化这些功能?

boost::function< void(int) > binder( void(*func)(int, char), int a1, char a2 )
{
    return boost::bind( func, a1, a2 );
}

void proxy( boost::function< void(int) > func, int a1 )
{
    boost::bind( func, a1 )();
}

我尝试了以下但没有成功:

template< typename R, typename A1, typename A2 >
static boost::function< void(int) > binder( R(*func)(A1,A2), A1 a1, A2 a2 )
{
    return boost::bind( func, a1, a2 );
}

template< typename A1 >
static void proxy( boost::function< void(A1) > func, A1 a1 )
{
    boost::bind( func, a1 )();
}

如果没有binder(),我会很高兴。这就是我打算使用它们的方式:

void print( int i, char c );
boost::signals2::signal.connect(
    boost::bind(
        &proxy,
        boost::bind(
            &binder,
            &print,
            _1,
            'a'
            ),
        _1
        )
    );

我检查了以下但没有运气:

how-to-use-manipulate-return-value-from-nested-boostbind

perform-argument-substitution-on-nested-boostbind-without-composition

can-i-use-boost-bind-with-a-function-template

1 个答案:

答案 0 :(得分:2)

你需要拼写正确的函数指针:

R(*func)(A1, A2)

您还需要指定用于形成函数指针的模板参数:请记住binder 不是一个函数,而是模板

&binder<void, int, char>
&proxy<int>

最后,你没有得到正确的信号变量。声明如下:

boost::signals2::signal<void(int)> sig;

然后使用:

sig.connect( /* all that stuff */ );