I am writing some scientific code. I have created a function "quadrature" which integrates over a triangle a given function provided in argument through a pointer function, basically:
Your pointer must also be to a function double (*)(int, double, double).
I am pretty sure a function must exactly much the function pointer in arguments for its address to be assigned to the pointer.
In then you wanted to use a function with just the two double arguments, you could just give it an unnamed and unused integer parameter to ensure compatibility with the pointer:
I don't see why not. When it doubt, compile and see for yourself ;)
And as I said, if you want to use your quadrature function with a function taking just two double arguments, you just have to remember to give that function an unused integer parameter so it matches the prototype.
#include <iostream>
int add(int a, int b) { return a+b; }
int mul(int a, int b) { return a*b; }
struct Function
{
typedefint (*FPTR)(int, int);
FPTR f;
int a;
Function(FPTR f_, int a_):
f(f_), a(a_) {}
intoperator()(int b) { return f(a,b); }
};
int main()
{
Function add_5(add,5);
std::cout << add_5(2) << std::endl;
std::cout << add_5(3) << std::endl;
Function mul_3(mul,3);
std::cout << mul_3(3) << std::endl;
std::cout << mul_3(4) << std::endl;
return 0;
}
Well, not naming a parameter has no effect on anything, you'll still have to pass three parameters.
And in particular, it would require changing the type of function quadrature takes as a parameter. That way you actually spilled the specialization onto the "general case", even though the "general case" probably has no idea what to pass for the first parameter.
@Athar Heh, I feel stupid now... Of course that doesn't work :/ I'm not sure why, but it seems that earlier I didn't quite register the fact that the OP was passing arguments to the function on the line where he took its address :S
@JCS It looks like I gave you stupid advice - sorry about that...