473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with templates and code generalization

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;

}

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.

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.

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.
It would also be nice if I could generalize Display in shuch a way
that I could apply it even for nonmember functions.

Is there a way to write templates of this kinds, that apply to
"generic" functions without too much effort?

Thank you.

Cheers
StephQ

May 5 '07 #1
12 2393
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
May 7 '07 #2
Sorry for the very late reply.
I have big problems in trying to get code that uses mem_fun1 to work.
Here below is a very simplifyed example:

#include <iostream>
#include <functional>

using namespace std;

template<typename F>
class Display {
public:
static void computePlot(std::ostream& ss, F f)
{
ss << f( 1.0 ) << endl;
}
};
template<class F>
void plot(std::ostream& os, F f) {
return Display<F>::computePlot(os, f);
}

class Doubler {
public:
double foo(double x) const { return 2*x; }
};
int main() {
plot(cout, mem_fun1(&Doubler::foo));

}
The error I'm getting is:
no instance of function template "std::mem_fun1" matches the argument
list
argument types are: (double (Doubler::*)(double) const)
plot(cout, mem_fun1(&Doubler::foo));

If I remove the const keyword from foo I still get the following
error:

error: no instance of function "std::mem_fun1_t<_Result, _Ty,
_Arg>::operator() [with _Result=double, _Ty=Doubler, _Arg=double]"
matches the argument list
argument types are: (double)
object type is: std::mem_fun1_t<double, Doubler, double>
ss << f( 1.0 ) << endl;
^
detected during:
instantiation of "void Display<F>::computePlot(std::ostream &, F)
[with F=std::mem_fun1_t<double, Doubler, double>]" at line 19
instantiation of "void plot(std::ostream &, F) [with
F=std::mem_fun1_t<double, Doubler, double>]"
Cheers
StephQ

May 14 '07 #3
StephQ wrote:
Sorry for the very late reply.
I have big problems in trying to get code that uses mem_fun1 to work.
Here below is a very simplifyed example:

#include <iostream>
#include <functional>

using namespace std;

template<typename F>
class Display {
public:
static void computePlot(std::ostream& ss, F f)
{
ss << f( 1.0 ) << endl;
}
};
template<class F>
void plot(std::ostream& os, F f) {
return Display<F>::computePlot(os, f);
}

class Doubler {
public:
double foo(double x) const { return 2*x; }
Drop the 'const'.
};
int main() {
plot(cout, mem_fun1(&Doubler::foo));
Change to

Doubler d;
plot(cout, bind1st(mem_fun1(&Doubler::foo), &d));

Otherwise you're trying to make the program call a non-static member
function without an instance.
>
}
The error I'm getting is:
no instance of function template "std::mem_fun1" matches the argument
list
argument types are: (double (Doubler::*)(double) const)
plot(cout, mem_fun1(&Doubler::foo));

If I remove the const keyword from foo I still get the following
error:

error: no instance of function "std::mem_fun1_t<_Result, _Ty,
_Arg>::operator() [with _Result=double, _Ty=Doubler, _Arg=double]"
matches the argument list
argument types are: (double)
object type is: std::mem_fun1_t<double, Doubler, double>
ss << f( 1.0 ) << endl;
^
detected during:
instantiation of "void Display<F>::computePlot(std::ostream &, F)
[with F=std::mem_fun1_t<double, Doubler, double>]" at line 19
instantiation of "void plot(std::ostream &, F) [with
F=std::mem_fun1_t<double, Doubler, double>]"
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 14 '07 #4
class Doubler {
public:
double foo(double x) const { return 2*x; }

Drop the 'const'.
Is there a specific reason to drop the const?
I would like to avoid it, as this would imply also dropping the const
qualifyer from nearly all other const member functions of the class
(as they use this function a lot).

>
int main() {
plot(cout, mem_fun1(&Doubler::foo));

Change to

Doubler d;
plot(cout, bind1st(mem_fun1(&Doubler::foo), &d));

Otherwise you're trying to make the program call a non-static member
function without an instance.
Why this example works?
Draw is not a static member function.

#include <iostream>
#include <vector>
#include <functional>

using namespace std;

class Shape {
public:
virtual void draw(){cout << "Drawing a shape\n"; };
};
class Triangle : public Shape {
void draw() {cout << "Drawing a triangle\n"; }
};

class Square : public Shape {
void draw() {cout << "Drawing a square\n"; }
};

int main() {
vector<Shape*shapes;
Triangle t1,t2;
Square s1, s2;
shapes.push_back(&t1);
shapes.push_back(&s1);
shapes.push_back(&t2);
shapes.push_back(&s2);

for_each( shapes.begin(), shapes.end(), mem_fun(&Shape::draw));
}

From:
http://www-h.eng.cam.ac.uk/help/tpl/...+/mem_fun.html

Thank you for your help.

Cheers
StephQ

May 14 '07 #5
StephQ wrote:
>>class Doubler {
public:
double foo(double x) const { return 2*x; }

Drop the 'const'.

Is there a specific reason to drop the const?
Because with the 'const' it doesn't copmile?... <shrugDunno...
I would like to avoid it, as this would imply also dropping the const
qualifyer from nearly all other const member functions of the class
(as they use this function a lot).
You'll need to dig this out yourself, I'm afraid. Or hope that somebody
else would chime in... James Kanze recently did a lot of correcting of
my posts, perhaps he's got some idea (which I am sure he has)?
>
>>
>>int main() {
plot(cout, mem_fun1(&Doubler::foo));

Change to

Doubler d;
plot(cout, bind1st(mem_fun1(&Doubler::foo), &d));

Otherwise you're trying to make the program call a non-static member
function without an instance.

Why this example works?
Not sure what you mean. You sound like if some other example works,
so should anything you code. You don't use 'for_each', do you? Why
then compare bread and cheese here? In the example below the 'shapes'
vector contains all the instances on which 'mem_fun' "operates". In
your example, you _needed_ to supply the instance.
Draw is not a static member function.
And it's non-const as well. So? You could even name your function
'draw', and it wouldn't make an iota of difference. The main element
here is the presence of _an_instance_ of your class. Here there is
a whole vector of them. In your code there wasn't _any_.
>
#include <iostream>
#include <vector>
#include <functional>

using namespace std;

class Shape {
public:
virtual void draw(){cout << "Drawing a shape\n"; };
};
class Triangle : public Shape {
void draw() {cout << "Drawing a triangle\n"; }
};

class Square : public Shape {
void draw() {cout << "Drawing a square\n"; }
};

int main() {
vector<Shape*shapes;
Triangle t1,t2;
Square s1, s2;
shapes.push_back(&t1);
shapes.push_back(&s1);
shapes.push_back(&t2);
shapes.push_back(&s2);

for_each( shapes.begin(), shapes.end(), mem_fun(&Shape::draw));
}

From:
http://www-h.eng.cam.ac.uk/help/tpl/...+/mem_fun.html
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 14 '07 #6
Because with the 'const' it doesn't copmile?... <shrugDunno...
I would like to avoid it, as this would imply also dropping the const
qualifyer from nearly all other const member functions of the class
(as they use this function a lot).

You'll need to dig this out yourself, I'm afraid. Or hope that somebody
else would chime in... James Kanze recently did a lot of correcting of
my posts, perhaps he's got some idea (which I am sure he has)?
I made a mistake with my code. Now it compiles with the const keyword.

Not sure what you mean. You sound like if some other example works,
so should anything you code. You don't use 'for_each', do you? Why
then compare bread and cheese here? In the example below the 'shapes'
vector contains all the instances on which 'mem_fun' "operates". In
your example, you _needed_ to supply the instance.
What I want to say is that in the example I posted the transform
function template doesn't need 'bind1st' .
You are right that I have to supply the instance (in the transform
function you supply the two iterators).

My objective is to be able to write something like:

plot( os, mem_fun( &HistogramBin::f ), this, interval );

instead of:

plot( os, std::bind1st( std::mem_fun( &HistogramBin::f ), this),
interval );

here os is reference to ostream.
'this' because I'm calling the plot function from a member function
inside the Histogram class.

I think that there should be a way to achieve this result, but
unfortunatley I'm not expert enough to discovert it myself.

Also I'm quite stuck about how to write a plot function such as:

plot( os, mem_fun( &HistogramBin::f ), parameter, this, interval );

where f is
double f(double x, double y)

and parameter is a double that fixes y.

bind2nd should be of help, but I'm not sure about how to proceed.

And it's non-const as well. So? You could even name your function
'draw', and it wouldn't make an iota of difference. The main element
here is the presence of _an_instance_ of your class. Here there is
a whole vector of them. In your code there wasn't _any_.
Sorry if I wasn't clear enough in the first time.
I hope this post clarifies the situation.

Cheers
StephQ

May 14 '07 #7
StephQ wrote:
[..]
What I want to say is that in the example I posted the transform
function template doesn't need 'bind1st' .
You are right that I have to supply the instance (in the transform
function you supply the two iterators).

My objective is to be able to write something like:

plot( os, mem_fun( &HistogramBin::f ), this, interval );

instead of:

plot( os, std::bind1st( std::mem_fun( &HistogramBin::f ), this),
interval );

here os is reference to ostream.
'this' because I'm calling the plot function from a member function
inside the Histogram class.
template<class F, class C>
void plot(std::ostream& os, F f, C c, Interval interval)
{
...
f(c, x);
...
}

That's what 'mem_fun1' is for.
I think that there should be a way to achieve this result, but
unfortunatley I'm not expert enough to discovert it myself.
Not sure what result you're hoping to achieve, to be honest with you.
Any time you need an interface, you should start by coding the way[s]
you're going to be invoking that interface. Essentially, you have
some place in your code from which you call 'plot' and some function
your 'plot' needs to call. And all you need to do is match them so
they work well with each other.
Also I'm quite stuck about how to write a plot function such as:

plot( os, mem_fun( &HistogramBin::f ), parameter, this, interval );

where f is
double f(double x, double y)

and parameter is a double that fixes y.

bind2nd should be of help, but I'm not sure about how to proceed.
You might want to look at Boost binders instead. 'bind2nd' is
very limited, it doesn't accept two arguments.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 14 '07 #8
On May 14, 7:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
StephQ wrote:
>class Doubler {
public:
double foo(double x) const { return 2*x; }
Drop the 'const'.
Is there a specific reason to drop the const?
Because with the 'const' it doesn't copmile?... <shrugDunno...
Because the committee forgot to specify versions of the
underlying classes for const functions.
I would like to avoid it, as this would imply also dropping the const
qualifyer from nearly all other const member functions of the class
(as they use this function a lot).
You'll need to dig this out yourself, I'm afraid. Or hope that somebody
else would chime in... James Kanze recently did a lot of correcting of
my posts, perhaps he's got some idea (which I am sure he has)?
Well, my real suggestion would be to use boost::bind:-). There
are enough restrictions with regards to the various function
pointer adaptors (member or not) to make them IMHO more or less
useless as they stand in the standard. (The fact that they
don't work if any of the function arguments is a reference, for
example.) The result is that I don't use them.

For the rest, I'd say you got it right. You need an instance to
call a (non-static) member function, which is what mem_fun does.

In the end, however, boost::bind is just a couple of magnitudes
easier to use, and will be part of the next version of the
standard, so that's the direction I'd suggest evolving.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 15 '07 #9
On May 15, 11:03 am, James Kanze <james.ka...@gmail.comwrote:
On May 14, 7:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
StephQ wrote:
>>class Doubler {
>>public:
>> double foo(double x) const { return 2*x; }
>Drop the 'const'.
Is there a specific reason to drop the const?
Because with the 'const' it doesn't copmile?... <shrugDunno...

Because the committee forgot to specify versions of the
underlying classes for const functions.
I would like to avoid it, as this would imply also dropping the const
qualifyer from nearly all other const member functions of the class
(as they use this function a lot).
You'll need to dig this out yourself, I'm afraid. Or hope that somebody
else would chime in... James Kanze recently did a lot of correcting of
my posts, perhaps he's got some idea (which I am sure he has)?

Well, my real suggestion would be to use boost::bind:-). There
are enough restrictions with regards to the various function
pointer adaptors (member or not) to make them IMHO more or less
useless as they stand in the standard. (The fact that they
don't work if any of the function arguments is a reference, for
example.) The result is that I don't use them.

For the rest, I'd say you got it right. You need an instance to
call a (non-static) member function, which is what mem_fun does.

In the end, however, boost::bind is just a couple of magnitudes
easier to use, and will be part of the next version of the
standard, so that's the direction I'd suggest evolving.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Thank you for your suggestion.
I will soon install and try boost.

It seems to me that with binders you can write template class/
functions that are very flexible: with only one version you cover:
member functions
static member functions / non member functions
functors (classes with () overloaded)

Cheers
StephQ

May 15 '07 #10
On May 15, 11:03 am, James Kanze <james.ka...@gmail.comwrote:
On May 14, 7:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
StephQ wrote:
>>class Doubler {
>>public:
>> double foo(double x) const { return 2*x; }
>Drop the 'const'.
Is there a specific reason to drop the const?
Because with the 'const' it doesn't copmile?... <shrugDunno...

Because the committee forgot to specify versions of the
underlying classes for const functions.
I would like to avoid it, as this would imply also dropping the const
qualifyer from nearly all other const member functions of the class
(as they use this function a lot).
You'll need to dig this out yourself, I'm afraid. Or hope that somebody
else would chime in... James Kanze recently did a lot of correcting of
my posts, perhaps he's got some idea (which I am sure he has)?

Well, my real suggestion would be to use boost::bind:-). There
are enough restrictions with regards to the various function
pointer adaptors (member or not) to make them IMHO more or less
useless as they stand in the standard. (The fact that they
don't work if any of the function arguments is a reference, for
example.) The result is that I don't use them.

For the rest, I'd say you got it right. You need an instance to
call a (non-static) member function, which is what mem_fun does.

In the end, however, boost::bind is just a couple of magnitudes
easier to use, and will be part of the next version of the
standard, so that's the direction I'd suggest evolving.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I just did some experimentation implementing numerical quadrature
formulas using:

template<class T, double (T::*F)(double) const>
double benchDoubleParameter(const T& t, double x)
{
return (t.*F)(x);
}

or

template<class F>
double benchSingleParameter(const F& f, double x)
{
return f( x );
}

+

mem_fun/mem_ref_fun and bind1st

to get the function values.

It turned out that the first alternative is three time faster than the
second one, so (at least in this situation) it seems that there is an
overhead in using the mem/fun + binder thing.
Could I expect an improvement using boost::bind ?
Ist there a specific reason on why this happends ?

Thank you

Regards
StephQ

May 15 '07 #11
StephQ wrote:
On May 15, 11:03 am, James Kanze <james.ka...@gmail.comwrote:
On May 14, 7:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
[...]
I just did some experimentation implementing numerical quadrature
formulas using:
template<class T, double (T::*F)(double) const>
double benchDoubleParameter(const T& t, double x)
{
return (t.*F)(x);
}
or
template<class F>
double benchSingleParameter(const F& f, double x)
{
return f( x );
}
+
mem_fun/mem_ref_fun and bind1st
to get the function values.
It turned out that the first alternative is three time faster than the
second one, so (at least in this situation) it seems that there is an
overhead in using the mem/fun + binder thing.
Could I expect an improvement using boost::bind ?
Ist there a specific reason on why this happends ?
It depends a lot on the implementation of your compiler, and how
good it is in optimizing. Normally, you're using a pointer to a
member function in both cases, and I wouldn't expect a great
difference. Except that... in the first case, the pointer to
member function is a template parameter, and thus a constant;
the compiler should normally have no problem optimizing the call
into a normal call (*should*---I wouldn't be surprized if some
compilers didn't). In the second, the pointer to function is a
variable in the generated class type. In order to do the same
optimization, the compiler has to track this variable, and make
sure that it is always the same. To be clearer: if you call
benchXxxParameter with the same object type, but with different
member functions, the first will instantiate the template twice,
once for each member function, specialized for that member
function; the second will only instantiate the template once,
determining at run-time which function to call.

I'm not sure how boost::bind implements this. If it uses the
address of the member function as a template parameter
somewhere in its implementation, it will probably be close to
the first; if it doesn't, it's performance will be closer to the
second. (In either case: I'm pretty sure boost::bind wraps the
call in more layers of abstraction. Which means that the
compiler will have to work a little harder to find the
optimization. And it's quite possible that the threashold at
which the compiler gives up is somewhere in these additional
layers, in which case, boost::bind will be significantly slower.
IMHO, that's really a problem with the compiler; boost::bind has
been accepted for inclusion in the next version of the standard,
and compilers should be targetting it, and taking it into
consideration when they establish such threasholds.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 16 '07 #12
It depends a lot on the implementation of your compiler, and how
good it is in optimizing. Normally, you're using a pointer to a
member function in both cases, and I wouldn't expect a great
difference. Except that... in the first case, the pointer to
member function is a template parameter, and thus a constant;
the compiler should normally have no problem optimizing the call
into a normal call (*should*---I wouldn't be surprized if some
compilers didn't). In the second, the pointer to function is a
variable in the generated class type. In order to do the same
optimization, the compiler has to track this variable, and make
sure that it is always the same. To be clearer: if you call
benchXxxParameter with the same object type, but with different
member functions, the first will instantiate the template twice,
once for each member function, specialized for that member
function; the second will only instantiate the template once,
determining at run-time which function to call.

I'm not sure how boost::bind implements this. If it uses the
address of the member function as a template parameter
somewhere in its implementation, it will probably be close to
the first; if it doesn't, it's performance will be closer to the
second. (In either case: I'm pretty sure boost::bind wraps the
call in more layers of abstraction. Which means that the
compiler will have to work a little harder to find the
optimization. And it's quite possible that the threashold at
which the compiler gives up is somewhere in these additional
layers, in which case, boost::bind will be significantly slower.
IMHO, that's really a problem with the compiler; boost::bind has
been accepted for inclusion in the next version of the standard,
and compilers should be targetting it, and taking it into
consideration when they establish such threasholds.)
Thank you for the exaustive explanation.

Best Regards
StephQ

May 16 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Robert Brown | last post by:
Is there a good approach to modelling many heterogeneous entity types with that have some attributes in common? Say I have entities "employees" which share some attibutes (e.g. firstname,...
41
by: Jordan | last post by:
While writing some code, I realized I had never developed a consistent pattern for checking errors from a method. I have two styles I jump back and forth between, but I'm wondering which is...
24
by: Gaijinco | last post by:
I found one of that problems all of us have solve when they begin programming: given 3 numbers print the greater and the lesser one of the set. I was trying to remember the if-then-else...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
82
by: Edward Elliott | last post by:
This is just anecdotal, but I still find it interesting. Take it for what it's worth. I'm interested in hearing others' perspectives, just please don't turn this into a pissing contest. I'm in...
42
by: gt8887b | last post by:
Hello! In his recent newsletter embedded expert Jack Ganssle says that programming students, as well as professional developers should readh "great code" (hight quality/well-crafted code that...
15
by: Dilip | last post by:
The subject is a bit misleading because I really can't figure out what the following code snippet is doing and would appreciate any help in deciphering it. I mean I can understand code-wise what...
2
by: shapper | last post by:
Hello, I am for days trying to apply a XSL transformation to a XML file and display the result in a the browser. I am using Asp.Net 2.0. Please, could someone post just a simple code example,...
5
by: ryanoasis | last post by:
Working on a C++ assignment and I cant figure out the problems I am having w/ Templates and Subclasses. I know there are issues with templates and certain compilers so I am not sure what the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.