函数指针

发布于 2024-09-16 18:30:52 字数 691 浏览 3 评论 0原文

我必须将函数传递给指针。为此,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戴着白色围巾的女孩 2024-09-23 18:30:52

诺诺诺这行不通。因为 boost::function<...> 有一个模板化构造函数来接受任何和所有类型。稍后检查与调用签名的兼容性。重载解析无法解决此问题。

另外,我认为您想传递 &obj 而不是 this。尝试显式转换:

Foo(boost::function<float ()>(boost::bind(&test::toCall, &obj)));

尽管这非常丑陋,所以您可能需要引入 typedef

void Foo(FloatHandler) { ... }
...
FloatHandler f(boost::bind(&test::toCall, &obj));
Foo(f);

或者最终您可以使 Foo 成为仅接受任何可调用类型 T 的模板。我怀疑这可能是最简单的,因为在一般情况下,我怀疑您不知道需要转换到什么 boost::function<...> 。对于想要返回 std::complex<> 的人又如何呢?所以...

template<typename T>
void Foo(T) { ... }
...
Foo(boost::bind(&test::toCall, &obj));

希望这有帮助。

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 of this. Try converting explicitly:

Foo(boost::function<float ()>(boost::bind(&test::toCall, &obj)));

This is utterly ugly though so you may want to introduce a typedef

void Foo(FloatHandler) { ... }
...
FloatHandler f(boost::bind(&test::toCall, &obj));
Foo(f);

Or ultimately you could make Foo a template that accepts just any callable type T. I suspect that may be the simplest, because in the general case i suspect you don't know to what boost::function<...> you need to cast to. And how about folks that want to return a std::complex<>. So...

template<typename T>
void Foo(T) { ... }
...
Foo(boost::bind(&test::toCall, &obj));

Hope this helps.

一影成城 2024-09-23 18:30:52

this 行中

Foo(boost::bind(&test::toCall, this));

,其类型为 Wrapper。但绑定找不到 toCall 方法。

这是一个固定版本(完整,在 g++ 4.3.2 上编译),这可能就是您想要做的:

#include <boost/bind.hpp>
#include <boost/function.hpp>

void Foo(boost::function<int()>) {}
void Foo(boost::function<float()>) {}
void Foo(boost::function<double()>) {}

struct test {
  float toCall() {return 0.0f;}
};

int main(int,char**) {
  test obj;
  boost::function<float()> tgt=boost::bind(&test::toCall,obj);
  Foo(tgt);
  return 0;
}

正如 AndreyT 的回答所述,bind 的返回类型是......有点奇怪,因此显式强制转换为适当的函数类型。

In the line

Foo(boost::bind(&test::toCall, this));

this is of type Wrapper. But the bind can't find a toCall 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:

#include <boost/bind.hpp>
#include <boost/function.hpp>

void Foo(boost::function<int()>) {}
void Foo(boost::function<float()>) {}
void Foo(boost::function<double()>) {}

struct test {
  float toCall() {return 0.0f;}
};

int main(int,char**) {
  test obj;
  boost::function<float()> tgt=boost::bind(&test::toCall,obj);
  Foo(tgt);
  return 0;
}

As AndreyT's answer notes, the return type of bind is... a bit odd, hence the explicit coercion to an appropriate function type.

山有枢 2024-09-23 18:30:52

boost::bind 不会返回 boost::function 对象。它返回一个未指定类型的对象,该对象可以用作具有相应数量参数的函子。

虽然 boost::function 可以根据 boost::bind 的结果进行转换构造,但这种情况下的重载决策对于 C++ 来说“太复杂”。 (删除了我的错误示​​例,它没有真正说明正确的问题)。

boost::bind does not return a boost::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 of boost::bind, the overload resolution in this case is "too complex" for C++. (Removed my bad example which didn't really illustrate the right problem).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文