最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C++ function callback for class function - Stack Overflow

programmeradmin0浏览0评论

I have below snipped code works properly. But in my opinion it shouldn't work.

#include <functional>

template <class T> void run(std::function<void(T *)> fun, T *obj) { fun(obj); }

struct Foo {
  void bar() {}
};

int main() {
  Foo foo;
  std::function<void(Foo *)> fun = &Foo::bar;

  run(fun, &foo);                     // works
}
  1. The bar() function is not compatible with the expected blueprint. bar() function accepts no parameter but std::functional accepts T *.
  2. fun variable is defined only &Foo::bar. How compiler knows, it is function of foo object?
发布评论

评论列表(0)

  1. 暂无评论