函数指针
我必须将函数传递给指针。为此,我使用 boost::function 。捕获指针的函数对于不同的签名是重载的。例如:
void Foo(boost::function<int ()>) { ... }
void Foo(boost::function<float ()>) { ... }
void Foo(boost::function<double ()>) { ... }
现在我想在那里传递一些类方法指针:
class test
{
public:
float toCall() { };
};
class Wrapper
{
Wrapper() {
test obj;
Foo(boost::bind(&test::toCall, this));
}
};
error: no matching function for call to ‘Foo(boost::_bi::bind_t<float, boost::_mfi::mf0<float, test>, boost::_bi::list1<boost::_bi::value<Wrapper*> > >)’
note: candidates are: Foo(boost::function<float()>&)
I have to pass function into pointer. For this purposes I'm using boost::function. The function which catches the pointer is overloaded for different signatures. For example:
void Foo(boost::function<int ()>) { ... }
void Foo(boost::function<float ()>) { ... }
void Foo(boost::function<double ()>) { ... }
Now I wanna pass some class-method pointer there:
class test
{
public:
float toCall() { };
};
class Wrapper
{
Wrapper() {
test obj;
Foo(boost::bind(&test::toCall, this));
}
};
error: no matching function for call to ‘Foo(boost::_bi::bind_t<float, boost::_mfi::mf0<float, test>, boost::_bi::list1<boost::_bi::value<Wrapper*> > >)’
note: candidates are: Foo(boost::function<float()>&)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
诺诺诺这行不通。因为
boost::function<...>
有一个模板化构造函数来接受任何和所有类型。稍后检查与调用签名的兼容性。重载解析无法解决此问题。另外,我认为您想传递
&obj
而不是this
。尝试显式转换:尽管这非常丑陋,所以您可能需要引入 typedef
或者最终您可以使
Foo
成为仅接受任何可调用类型T
的模板。我怀疑这可能是最简单的,因为在一般情况下,我怀疑您不知道需要转换到什么boost::function<...>
。对于想要返回std::complex<>
的人又如何呢?所以...希望这有帮助。
Nonono this cannot work. Because
boost::function<...>
has a templated constructor to accept any and all types. Compatibility with the call signature is checked later on. Overload resolution cannot resolve this.Also, i think you want to pass
&obj
instead ofthis
. Try converting explicitly:This is utterly ugly though so you may want to introduce a typedef
Or ultimately you could make
Foo
a template that accepts just any callable typeT
. I suspect that may be the simplest, because in the general case i suspect you don't know to whatboost::function<...>
you need to cast to. And how about folks that want to return astd::complex<>
. So...Hope this helps.
在
this
行中,其类型为
Wrapper
。但绑定找不到toCall
方法。这是一个固定版本(完整,在 g++ 4.3.2 上编译),这可能就是您想要做的:
正如 AndreyT 的回答所述,bind 的返回类型是......有点奇怪,因此显式强制转换为适当的函数类型。
In the line
this
is of typeWrapper
. But the bind can't find atoCall
method on it.Here's a fixed-up version (complete, compiles on g++ 4.3.2) which is probably what you're trying to do:
As AndreyT's answer notes, the return type of bind is... a bit odd, hence the explicit coercion to an appropriate function type.
boost::bind
不会返回boost::function
对象。它返回一个未指定类型的对象,该对象可以用作具有相应数量参数的函子。虽然
boost::function
可以根据boost::bind
的结果进行转换构造,但这种情况下的重载决策对于 C++ 来说“太复杂”。 (删除了我的错误示例,它没有真正说明正确的问题)。boost::bind
does not return aboost::function
object. It returns an object of unspecified type that can be used as a functor with corresponding number of parameters.While
boost::function
can be conversion-constructed from the result ofboost::bind
, the overload resolution in this case is "too complex" for C++. (Removed my bad example which didn't really illustrate the right problem).