In my class called Mat
, I want to have a function which takes another function as a parameter. Right now I have the 4 functions below, but I get an error in when calling print(). The second line gives me an error, but I don't understand why, since the first one works. The only difference is function f
is not a member of the class Mat
, but f2
is.
The failure is: error: no matching function for call to Mat::test( < unresolved overloaded function type>, int)'
template <typename F>
int Mat::test(F f, int v){
return f(v);
}
int Mat::f2(int x){
return x*x;
}
int f(int x){
return x*x;
}
void Mat::print(){
printf("%d\n",test(f ,5)); // works
printf("%d\n",test(f2 ,5)); // does not work
}
Why does this happen?
The type of pointer-to-member-function
is different from pointer-to-function
.
The type of a function is different depending on whether it is an ordinary function or a non-static member function of some class:
int f(int x);
the type is "int (*)(int)" // since it is an ordinary function
And
int Mat::f2(int x);
the type is "int (Mat::*)(int)" // since it is a non-static member function of class Mat
Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)"
In C++, member functions have an implicit parameter which points to
the object (the this pointer inside the member function). Normal C
functions can be thought of as having a different calling convention
from member functions, so the types of their pointers
(pointer-to-member-function vs pointer-to-function) are different and
incompatible. C++ introduces a new type of pointer, called a
pointer-to-member, which can be invoked only by providing an object.
NOTE: do not attempt to "cast" a pointer-to-member-function into a
pointer-to-function; the result is undefined and probably disastrous.
E.g., a pointer-to-member-function is not required to contain the
machine address of the appropriate function. As was said in the last
example, if you have a pointer to a regular C function, use either a
top-level (non-member) function, or a static (class) member function.
More on this Here and here.
The problem here is that f2
is a method on Mat
, while f
is just a free function. You can't call f2
by itself, it needs an instance of Mat
to call it on. The easiest way around this might be:
printf("%d\n", test([=](int v){return this->f2(v);}, 5));
The =
there will capture this
, which is what you need to call f2
.