boost绑定类函数指针
class Foo
{
double f1( int x, std::string s1 );
double f2( int x, SomeClass s2 );
}
我希望能够绑定 Foo.f1 的 s1,而无需在 essense 中创建 foo 实例
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 最终被绑定
void SomeOtherFunction( MyFooFunc func )
{
Foo foo;
boost::function< double (int) > finalFunc =
boost::bind( func, foo, _1 );
}
: 这可能吗?如果是,1)如何实现? 2)MyFooFunc的声明是什么?
class Foo
{
double f1( int x, std::string s1 );
double f2( int x, SomeClass s2 );
}
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 );
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 );
}
Questions:
Is this possible? If yes, 1) how to achieve it? 2) What's the declaration of MyFooFunc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boost::bind
的结果不是指向成员的指针,因此func1
无法在第二行进行初始化。boost::bind
的结果是未指定的类型(这将取决于参数)。如果您使用的是 C++0x,命名bind
调用结果的最简单方法是使用auto
:另一种简单的方法(不限于 C+ +03) 只是不命名结果,而是当场使用它:
或者,您可以使用类型擦除将
boost::bind
的结果存储到boost 中: :function
,你似乎很熟悉。boost::function
是一种可能性,但不是唯一的选择。我们现在需要为
SomeOtherFunction
找到适当的签名:同样,指向成员的指针无法从调用boost::bind
的结果中初始化,因此< code>void SomeOtherFunction(MyFooFunc func); 不起作用。您可以将该函数设为模板:如果模板不合适,那么您必须使用某种类型擦除,例如
boost::function
。(同样,其他
boost::function
类型也是可能的,具体取决于细节,例如传递 ref-to-const 而不是 ref-to-non-const)The result of
boost::bind
is not a pointer to member, sofunc1
cannot be initialized as such on the second line. The result ofboost::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 tobind
is to useauto
:Another simple way (not restricted to C++03) is simply to not name the result, but to use it on the spot:
Or, you can use type-erasure to store the result of
boost::bind
into aboost::function
, which you seem to be familiar with.boost::function<double(Foo&, int)>
is a possibility but not the only choice.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 toboost::bind
, sovoid SomeOtherFunction(MyFooFunc func);
won't work. You can make the function a template instead:If a template is not preferrable, then you must use some kind of type-erasure such as, again,
boost::function
.(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)试试这个:
object
是 Foo 类的一个实例。_1 和 _2 是参数占位符。
Try this:
object
is an instance of class Foo._1 and _2 are the argument placeholders.