Re: Help with templates and code generalization
StephQ wrote:
I face the following problem.
I wrote a poor's man plotting function: computePlot. This function
append some x-values belonging to [xmin,xmax] step and the
correseponding f( x ) (for a given const member function f that takes
and return a double) values to a reference stringstream, let's call
this ss.
Then I usually use
ss.str()
to transfer the results in a file and plot these results using an
external software like gnuplot.
Because I'm using this plotting function in a lot of situations I
decided to write it in template form. Here is a simplifyed version:
template <class T, double (T::*F)(double) const>
class Display
{
private:
static double resolution;
public:
static void computePlot(std::stringstream& ss, const T& t, const
ClosedInterval& interval);
};
template <class T, double (T::*F)(double) const>
double Display<T,F>::resolution = 0.01;
template <class T, double (T::*F)(double) const>
void Display<T,F>::computePlot(std::stringstream& ss, const T& t,
const ClosedInterval& interval)
{
double xMin = interval.getLower();
double xMax = interval.getUpper();
double lenght = xMax - xMin;
int n = static_cast<int> (lenght/resolution);
if (n < 2)
{
n = 2;
}
double delta = lenght/(n-1);
/* We print the first and last value separately to avoid numerical
issues. */
ss << xMin << " " << (t.*F)( xMin ) << std::endl;
double x;
int i;
for (i = 1 ; i < n-1 ; i++)
{
x = xMin + delta*i;
ss << x << " " << (t.*F)( x ) << std::endl;
}
ss << xMax << " " << (t.*F)( xMax ) << std::endl;
}
Next time, please don't format your code using tab characters...
The problem is that this template expect a second template parameter
of the form:
double (T::*F)(double) const
a pointer to const member function that returns a double and takes a
double.
double (T::*F)(double) already requires a rewriting of the whole
Display class.
Ah, so if it's a non-const member, you cannot use it, right?
As efficency is not a concern with this part of the code maybe I sould
use a generic second template parameter:
class F
but then I have no idea of how to procede.
The problem is that you'd be forcing the user of your class (yourself
at the very least) to use adapters like 'mem_fun' to use your 'Display'
template with member functions. Not such a big deal, but still a bit
tedious.
To sum up, the problem I'm trying to solve is to find a way that
minimize code rewriting while allowing me to apply this poor's man
plotting to different types of member functions of a generic class.
For example I would like to be able to apply computePlot to a member
function that returns a dobule but takes two doubles: f( xvalue,
parameter ) applying the alorithm in computePlot for a fixed parameter
value.
That would require still more work on the outside of 'Display' class
to adapt the two-argument function to use with one argument (which the
'Display' class will provide. Possible.
It would also be nice if I could generalize Display in shuch a way
that I could apply it even for nonmember functions.
If you give it 'F' as you described here, it's fine with non-member
functions.
Is there a way to write templates of this kinds, that apply to
"generic" functions without too much effort?
Unfortunately, no.
In your solution 'T' is not used for anything except to form the call
to 'F' member, right. So, merge them into one F and force the user
to utilise binders (either standard or of his own device):
/* note that I've replaced 'stringstream' with 'ostream')
the caller is then free to pass _any_ stream there */
template<typename F, typename Interval>
class Display {
static void computePlot(std::ostream& ss,
F f, Interval interval)
{
... f(xMin) ...
... f(x) ...
... f(xMax) ...
}
};
... // resolution, etc.
template<class F, class Interval>
void plot(std::ostream& os, F f, Interval i) {
return Display<F>::computePlot(os, f, i);
}
struct Doubler {
double foo(double x) { return 2*x; }
};
#include <cmath>
int main() {
Doubler d;
plot(cout, bind1st(mem_fun1(&Doubler::foo), d), ..
plot(cout, std::sin, ..
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask