Boost函数和Boost Bind:绑定返回值?
这与上一个问题相关: 将 boost::bind 与 boost 结合使用: :function: 检索绑定变量类型。
我可以绑定这样的函数:
in .h:
class MyClass
{
void foo(int a);
void bar();
void execute(char* param);
int _myint;
}
in .cpp
MyClass::bar()
{
vector<boost::function<void(void)> myVector;
myVector.push_back(boost::bind(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* param)
{
boost::function<void(void)> f = myVector[0];
_myint = atoi(param);
f();
}
但如何绑定返回值?即:
在.h中:
class MyClass
{
double foo(int a);
void bar();
void execute(char* param);
int _myint;
double _mydouble;
}
在.cpp中
MyClass::bar()
{
vector<boost::function<void(void)> myVector;
//PROBLEM IS HERE: HOW DO I BIND "_mydouble"
myVector.push_back(boost::bind<double>(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* param)
{
double returnval;
boost::function<void(void)> f = myVector[0];
_myint = atoi(param);
//THIS DOES NOT WORK: cannot convert 'void' to 'double'
// returnval = f();
//MAYBE THIS WOULD IF I COULD BIND...:
// returnval = _mydouble;
}
This is related to this previous question: Using boost::bind with boost::function: retrieve binded variable type.
I can bind a function like this:
in .h:
class MyClass
{
void foo(int a);
void bar();
void execute(char* param);
int _myint;
}
in .cpp
MyClass::bar()
{
vector<boost::function<void(void)> myVector;
myVector.push_back(boost::bind(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* param)
{
boost::function<void(void)> f = myVector[0];
_myint = atoi(param);
f();
}
But how can I bind a return value ? i.e.:
in .h:
class MyClass
{
double foo(int a);
void bar();
void execute(char* param);
int _myint;
double _mydouble;
}
in .cpp
MyClass::bar()
{
vector<boost::function<void(void)> myVector;
//PROBLEM IS HERE: HOW DO I BIND "_mydouble"
myVector.push_back(boost::bind<double>(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* param)
{
double returnval;
boost::function<void(void)> f = myVector[0];
_myint = atoi(param);
//THIS DOES NOT WORK: cannot convert 'void' to 'double'
// returnval = f();
//MAYBE THIS WOULD IF I COULD BIND...:
// returnval = _mydouble;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要的是一个返回
void
但在执行此操作之前使用foo()
的结果为_myDouble
赋值的空函数,那么仅使用 Boost.Bind 无法轻松做到这一点。然而,Boost 有另一个专门针对此类事情的库 - Boost.Phoenix:If what you want is a nullary function that returns
void
but assigns a value to_myDouble
with the result offoo()
before doing so, then you cannot do this easily with just Boost.Bind. However, Boost has another library specifically catered to this sort of thing -- Boost.Phoenix:问题 1:
myVector
需要是类成员。问题 2:
myVector
对返回双精度且不带参数的函数感兴趣,这将是boost::function
然后,将 _mydouble 绑定到foo 的参数,调用
boost::bind(&MyClass::foo, this, MyClass::_mydouble)
,当 foo 为叫。Boost.Bind 可以提供的最接近的方法是提供 toreturn 作为参数。
problem 1:
myVector
needs to be a class member.problem 2:
myVector
is interested in functions that return doubles and take no arguments, which would beboost::function<double()>
then, to bind _mydouble to the parameter of foo, call
boost::bind(&MyClass::foo, this, MyClass::_mydouble)
which should give you a compilation warning about casting a double to an int for when foo is called.The closest you can come with Boost.Bind is providing the toreturn as a parameter.