Function Objects
I started writing this template after reading Qiang Liu's C/C++ Users Journal article [2] on the use of function objects for solving a Newton-Raphson problem. Function objects are just classes that implement a "call operator" [ operator ( ) (. . .)]. For Liu's problem, simple function objects that are exactly drop-in replacements for C's function pointers elegantly solved his problem: how to have different function signatures for different problems without hardcoding them. Function objects also provide a convenient method for holding data specific to the problem being solved. So you get several benefits from using function objects: no hardcoded function signatures, a convenient place to store problem-specific data, and compatibility with pointers to functions (for a C-like solution to function evaluations).
Modeling my solution after Liu's, I wrote a template for the simplex method that used a function object for function evaluation. Unlike Liu, I needed at least three different kinds of information from the parameterized class: the function value evaluated at some point, a starting point for the simplex, and the number of variables in the simplex. I solved this by making function object's class more complex. I implemented two different call operators, using the overloading ability of C++.
double operator ( ) ( const T t ); T operator ( ) ( );
So the function evaluation at a point is returned if a point is supplied. If no parameters are supplied, then a vector of the parameters is returned. In my case, I returned the starting point so that the simplex could be told where to start. Since I used std::valarray to hold the parameter values, the same call to the call operator that returns the starting point to the simplex template can use:
m_nVar = t.size( );to determine how many parameters there are in the problem. The simplex template needs that value to compose m_nVar+1 points that make up the simplex and to assign the size of each of those points (stored in valarrays). Any program that uses this simplex template will need to implement both of these call operators.
Why function object? Originally, the idea was as a simple replacement for pointers to functions. An important result of using function objects is that the source code (template in this case) is not dependent on a particular set of function signatures.
L.A.