www.digitalmars.com         C & C++   DMDScript  

D - Can D fix the 'template bubbling' problem?

reply Linas Vepstas <Linas_member pathlink.com> writes:
One objection/difficulty I've had with generic programing and templates 
in C++
was the 'bubbling templates' problem.  That is, is class x uses 
class y which
uses class z, and class z is a template/generic program, 
then, to expose this
genericism, you have to make x and y be tamplates 
as well, even if they
otherwise didn't need to be. (for example, if 
they are merely manipulating
references instead of touching the actual 
type. 
 
Well, by golly, here's a
language that just might be able to fix this 
mis-feature. 
 
I've sometimes
daydreamed that adding 'lambda' to C could fix this problem. 
I'm sad to see no
mention of lambda :-( 
 
As an alternative (instead of lambda, which has its own
difficulties),  
here's another way to describe what I really want to do:   
 
I
want to be able to pass "function pointers" of a very definite and  
fixed
signature to other routines, and yet, when this "function" is 
finally called,
the compiler realizes that its actually a method call 
on some object instance,
and that method is called instead.  So, for  
pseudo-C/C++ borken example:
double newtons_method_solver (double (*f)(double) ) 
{ 
   ... code to find
zeros of f... 
} 
 
class my_crazy_func { 
   double a,b,c; 
public: 
   double
getvalue(double x) { return a*x*x+b*x+c; } 
}; 
 
main () {  
 
   ...
setup/init code... 
   // clearly the following won't work, but you catch my
drift.... 
   double a = newtons_method_solver (my_crazy_func::getvalue);
printf ("the answer is %f\n", a); 
 } 
 
The point of the example being that
newtons_method_solver() really really 
really does NOT need to be a template
class, for many reasons. 
Yet I want to be able to call it on functions with
'hidden' arguments, 
such as the 'hidden' this pointer in the above example.
Today, in C++, 
I am "forced" to use templates to solve this stupid problem.
When I say "I want lambda" I really mean "I want to bind object instances 
to
function pointers" so that I can program as above. 
 
Makes sense?  Let me know
.. <linas linas.org> 
 
--linas 
 
Apr 19 2004
parent "C. Sauls" <ibisbasenji yahoo.com> writes:
Linas Vepstas wrote:
 I
 want to be able to pass "function pointers" of a very definite and  
 fixed
 signature to other routines, and yet, when this "function" is 
 finally called,
 the compiler realizes that its actually a method call 
 on some object instance,
 and that method is called instead.  So, for  
 pseudo-C/C++ borken example:
 double newtons_method_solver (double (*f)(double) ) 
 { 
    ... code to find
 zeros of f... 
 } 
  
 class my_crazy_func { 
    double a,b,c; 
 public: 
    double
 getvalue(double x) { return a*x*x+b*x+c; } 
 }; 
  
 main () {  
  
    ...
 setup/init code... 
    // clearly the following won't work, but you catch my
 drift.... 
    double a = newtons_method_solver (my_crazy_func::getvalue);
 printf ("the answer is %f\n", a); 
  }

I think this is what you're trying to accomplish... ------------------------------ double newtons_method_solver (double delegate(double) dg) { ... } class my_crazy_func { private double a, b, c; double getvalue(double x) { return a * x * x + b * x + c; } } int main () { // . . . my_crazy_func mcf = new my_crazy_func; double a = newtons_method_solver(&mcf.getvalue); } ------------------------------ -C. Sauls -Invironz
Apr 19 2004