MCP
MCP

Reputation: 4536

difference between function parameters

In the parameter's of a function, I want to pass a default argument that is a function template. What I'm trying to decipher is the difference between (*cmp) or (cmp) in the function below:

template <typename Type>
int Foo(some var, int (*cmp)(Type one, Type two) = FunctTemplate) { ...

I am used to seeing the * as a pointer declaration... Is this a pointer to the function FunctTemplate? Why does the program see to work regardless of way I write it (astrik or no astrik)?

Upvotes: 1

Views: 102

Answers (1)

James McNellis
James McNellis

Reputation: 355049

The types are not the same, but there is no difference when they are used as a parameter type in a function declaration.

In int (*cmp)(Type, Type), cmp has pointer-to-function type (or a "function pointer" type).

In int (cmp)(Type, Type), cmp has function type (i.e., it is not a pointer type at all).

However, C and C++ both have a rule that any parameter that has a function type is implicitly converted to the corresponding function pointer type, just as any parameter that has an array type is implicitly converted to the corresponding pointer type.

Upvotes: 3

Related Questions