Reputation: 1555
i've got a function declaration in c++ and need to know how it's working:
template<class x>
int fun(x, x(*)(x*) );
The first arg is an object of type x. And how to describe the second one?
Upvotes: 1
Views: 234
Reputation: 249143
The second parameter type, where x is a class:
x(*)(x*)
Means "a pointer (*)
to a function returning x
and taking x*
. For example:
class MyClass {};
MyClass doit(MyClass* arg) { return *arg; }
MyClass instance;
int result = fun(instance, doit);
Upvotes: 4
Reputation: 75130
That is the declaration of a template function that returns an integer and takes, as parameters, an x
and a pointer to a function that returns an x
and takes, as a parameter, an x*
.
The part x(*)(x*)
is the part that means "a pointer to a function that returns an x
and takes, as a parameter, an x*
". The first x
is the return type, the (*)
indicates that it is a pointer to a function (if the parameter had a name, it would be written x(*argname)(x*)
), and the third x*
is just the argument.
Calling it would look like this:
int f(int* iptr) { return something; }
fun(4, f); // no need for the explicit template parameter because it can be deduced
Or more generally
template<typename x>
x functionname(x* xptr) { return something; }
It will not work in C because, as Daniel White said in a comment, C doesn't have templates.
Upvotes: 5
Reputation: 14612
It is a pointer to a function that return the object of type x, and got one parameter of a pointer to the type x.
Upvotes: 1