How to pass operators as parameters
Posted
by Rodion Ingles
on Stack Overflow
See other posts from Stack Overflow
or by Rodion Ingles
Published on 2010-03-31T10:25:26Z
Indexed on
2010/03/31
10:33 UTC
Read the original article
Hit count: 240
c++
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:
MultiplyData(double data)
{ TransformData(data, &(operator *)); }
DivideData(double data)
{ TransformData(data, &(operator /)); }
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:
MultiplyData(double data)
{
std::multiplies<double> multFunct;
TransformData(data, &multFunct);
}
DivideData(double data)
{
std::divides<double> divFunct;
TransformData(data, &divFunct);
}
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)?
© Stack Overflow or respective owner