Suppose I have the following function, the value it produces is needed by many other functions in the code:
float mean(foo param1, bar param2);
My other functions look like this:
float foobar(foo param1, bar param2, float meanValue);
What I want to do is the following:
float foobar(foo param1, bar param2, float meanValue = mean(param1, param2));
While passing in a function as a default parameter is legal, the compiler (VS2008) complains that param1 and param2 are not declared - because the parameters get pushed onto the stack right-to-left, starting with meanValue. Of course, I can't reverse the order of parameters, since in that case I wouldn't be able to specify a default value for the first parameter.
I could pass in some value like -10000 for the mean to tell myself to call the mean() function; or I could overload every function (and there are 10+ to overload), but that's not very neat. I think it's really cool that you can call a function as a default parameter and I'm wondering if there's a neat way to accomplish what I'm trying to do.
In which order parameters are pushed to the stack, or whether they are pushed at all, or whether there even is a stack on the architecture, is not specified by the C++ standard.
You don't give much information about why you have so many such functions, and how they differ, but maybe the following would help reduce the number of overloads:
template< typename T1, typename T2 >
float foobar(T1 param1, T2 param2)
{
return foobar(param1, param2, mean(param1, param2));
}
Note that you can make the result type and the function called, and the mean()
function, all template parameters, too, if you need to.