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
}
- The
bar()
function is not compatible with the expected blueprint.bar()
function accepts no parameter butstd::functional
acceptsT *
. fun
variable is defined only&Foo::bar
. How compiler knows, it is function offoo
object?