本文介绍了boost绑定类函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Foo 
{
    double f1( int x, std::string s1 );
    double f2( int x, SomeClass s2 );
}



我想要能绑定Foo.f1的s1没有foo的实例

I want to be able to bind Foo.f1's s1 without an instance of foo to create in essense

typedef double (Foo::* MyFooFunc)( int ) 

MyFooFunc func1 = boost::bind( &Foo::f1, _1, _2, "some string" );
MyFooFunc func2 = boost::bind( &Foo::f2, _1, _2, SomeClass );

然后我将func1和func2作为参数传递给其他函数,其中Foo终于绑定:

Then I pass func1 and func2 as parameters to other functions, inside which Foo is finally bound:

void SomeOtherFunction( MyFooFunc func )
{
     Foo foo;
     boost::function< double (int) > finalFunc =
          boost::bind( func, foo, _1 );
}

问题:
这是可能吗?如果是,1)如何实现呢? 2)什么是MyFooFunc的声明?

Questions:Is this possible? If yes, 1) how to achieve it? 2) What's the declaration of MyFooFunc?

推荐答案

typedef double (Foo::* MyFooFunc)( int );

MyFooFunc func1 = boost::bind( &Foo::f1, _1, _2, "some string" );

boost :: bind 的结果是不是指向成员的指针,因此 func1 不能像第二行那样初始化。 boost :: bind 的结果是未指定的类型(这将取决于参数)。如果你使用C ++ 0x,调用 bind 的最简单的方法是使用 auto

The result of boost::bind is not a pointer to member, so func1 cannot be initialized as such on the second line. The result of boost::bind is an unspecified type (which will depend on the parameters). If you're using C++0x, the simplest way to name the result of a call to bind is to use auto:

auto func1 = boost::bind( &Foo::f1, _1, _2, "some string" );

另一种简单的方法(不限于C ++ 03)现在使用它:

Another simple way (not restricted to C++03) is simply to not name the result, but to use it on the spot:

SomeOtherFunction(boost::bind(&Foo::f1, _1, _2, "some string"));

或者,可以使用type-erasure存储 boost的结果: :bind 添加到您似乎熟悉的 boost :: function 中。 boost :: function< double(Foo& int)> 是一种可能性,但不是唯一的选择。

Or, you can use type-erasure to store the result of boost::bind into a boost::function, which you seem to be familiar with. boost::function<double(Foo&, int)> is a possibility but not the only choice.

我们现在需要为 SomeOtherFunction 找到适当的签名:再次,指向成员的指针不能从结果初始化调用 boost :: bind ,所以 void SomeOtherFunction(MyFooFunc func); 将无法工作。您可以使该函数成为模板:

We now need to find the appropriate signature for SomeOtherFunction: again, a pointer to member can't be initialized from the result of a call to boost::bind, so void SomeOtherFunction(MyFooFunc func); won't work. You can make the function a template instead:

template<typename Func>
void SomeOtherFunction( Func func )
{
     Foo foo;
     boost::function< double (int) > finalFunc =
          boost::bind( func, foo, _1 );
}

如果模板不是首选,那么必须使用某种类型 - 擦除,例如, boost :: function

If a template is not preferrable, then you must use some kind of type-erasure such as, again, boost::function.

void SomeOtherFunction(boost::function<double(Foo&, int)> const& func);

(另一次 boost :: function 类型是可能的,取决于细节,例如传递一个ref-to-const而不是一个ref-to-non-const)

(once again other boost::function types are possible depending on details such as passing a ref-to-const as opposed to a ref-to-non-const)

这篇关于boost绑定类函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 22:28