I have to load an array of doubles
from a file, multiply each element by a value in a table (different values for different elements), do some work on it, invert the multiplication (that is, divide) and then save the data back to file.
Currently I implement the multiplication and division process in two separate methods. Now there is some extra work behind the scenes but apart from the specific statements where the multiplication/division occurs, the rest of the code is identical. As you can imagine, with this approach you have to be very careful making any changes. The surrounding code is not trivial, so its either a case of manually editing each method or copying changes from one method to the other and remembering to change the * and / operators.
After too many close calls I am fed up of this and would like to make a common function which implements the common logic and two wrapper functions which pass which operator to use as a parameter.
My initial approach was to use function pointers:
void MultiplyData(double data)
{ TransformData(data, &(operator *)); }
void DivideData(double data)
{ TransformData(data, &(operator /)); }
void TransformData(double data, double (*func)(double op1, double op2))
{ /* Do stuff here... */ }
However, I can't pass the operators as pointers (is this because it is an operator on a native type?), so I tried to use function objects. Initially I thought that multiplies
and divides
functors in <functional>
would be ideal:
void MultiplyData(double data)
{
std::multiplies<double> multFunct;
TransformData(data, &multFunct);
}
void DivideData(double data)
{
std::divides<double> divFunct;
TransformData(data, &divFunct);
}
void TransformData(double data, std::binary_function<double, double, double> *funct)
{ /* Do stuff here... */ }
As you can see I was trying to use a base class pointer to pass the functor polymorphically. The problem is that std::binary_function
does not declare an operator()
member for the child classes to implement.
Is there something I am missing, or is the solution to implement my own functor heirarchy (which really seems more trouble than it is worth)?
Operators can't be passed like that, and they can't generally be substited by variables, so operators suck that way.
Is it possible to pass an operator like ^(XOR) or ||(OR) or &&(AND), etc in a function/method? You could pass in a function (like operator||). If you do, though, keep in mind that operator||('f', 'f') is true.
A function can also be passed to another function by passing its address to that function; In simple terms, it could be achieved via pointers. Example: C++
Make TransformData a template function:
template <typename F>
typename F::result_type TransformData(double data, F f) { ... }
Call it thus:
double MultiplyData(double data) {
return TransformData(data, std::multiplies<double>());
}
std::binary_function is a tagging class. It's primary purpose is not to provide a base class interface, but to inject some typedefs into functor-style classes (via inheritance), which makes them usable by other parts of the standard library.
You might want to look at using Boost.Bind or Boost.Function so that you can do something like this:
TransformData(boost::function<double(double, double)> func) {
// use func like a function that
// returns a double and takes two
// double parameters
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With