admin管理员组

文章数量:1334856

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?

本文标签: C function callback for class functionStack Overflow