How to pass operators as parameters
<p>I have to load an array of <code>doubles</code> 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.</p>

<p>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.</p>

<p>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.</p>

<p>My initial approach was to use function pointers:</p>

<pre><code>void MultiplyData(double data)  
{ TransformData(data, &amp;(operator *));  } 

void DivideData(double data)  
{ TransformData(data, &amp;(operator /));  }  

void TransformData(double data, double (*func)(double op1, double op2))  
{  /* Do stuff here... */ } 
</code></pre>

<p>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 <code>multiplies</code> and <code>divides</code> functors in <code>&lt;functional&gt;</code> would be ideal:</p>

<pre><code>void MultiplyData(double data)  
{  
    std::multiplies&lt;double&gt; multFunct;  
    TransformData(data, &amp;multFunct);  
}

void DivideData(double data)  
{  
    std::divides&lt;double&gt; divFunct;  
    TransformData(data, &amp;divFunct);  
}

void TransformData(double data, std::binary_function&lt;double, double, double&gt; *funct)  
{  /* Do stuff here... */ } 
</code></pre>

<p>As you can see I was trying to use a base class pointer to pass the functor polymorphically. The problem is that <code>std::binary_function</code> does not declare an <code>operator()</code> member for the child classes to implement.</p>

<p>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)?</p>
