C++ wrapping

linas@austin.ibm.com linas@austin.ibm.com
Tue Apr 13 23:50:00 GMT 2004


On Mon, Apr 12, 2004 at 01:46:38PM -0600, Gerard Jungman wrote:
> On Mon, 2004-04-12 at 10:11, linas@austin.ibm.com wrote:
> > 
> > Sure. Pretend it was c not c++:
> > 
> > class GSLSolver 
> > {
> >    public:
> >       virtual void declare_func_to_solve (double (*f)(double));
> >       virtual double get_solution(void);
> >   private:
> >       double (*fs)(double);
> > };
> 
> This is no good because there is no place to put any underlying
> state (parameters) for the function. I see that as the main
> problem with function pointers.

Yes.  I see now I misunderstood the original email in several 
different ways ... 

> By the way, one way to get around this is to have an implementation
> of 'lambda', so you can bind other args to create a function with
> the signature you want. There are some C++ libraries that provide

Got my attention now; googling this was certainly fun.
I promptly googled on over to monads, but that's a different matter.

> something like this. However, even if we had a real and true
> standardized lambda in C++, I would not consider it a complete
> solution. 

Out of curiosity, why not? 

----------
Either way, I don't quite see why you'd need a full-fledged
lambda.  If I understand it correctly, the problem to be solved 
is how to get a solver to call a particular method on some
object, the object type being unknown to the caller.

This kind of problem has been worked around in GUI widget sets
for a long time now, in C and C++.  For example, in C, GTK programers
use 'glib', and in particular the g_signal's to make arbitrary
callbacks to unknown types.   But I can see how this seems to be
a bit messy, and that only C GUI programmers would find it acceptable.

However, I might think that the C++ version, with the right amount 
of sugar added, could look pretty smooth from the user's point of 
view.  e.g. If I remember correctly (it was long ago) both Qt (the
thing KDE is built on) and SGI's Open Inventor had a pretty slick 
way of hooking up 'signals'.  I'm not sure, though, it might have 
been little more than run-time upcasting, which I understand C++ 
now supports (?)

Actually it was a bit more ... at least Open Inventor also had some
manual reference counting, for memory management.  One can also
overload the unary * and & operators in C++ and thus turn plain-old
pointers into smart pointers and do memory management (or at least
reference counting) automatically.

So, to summarize, my current remark would then be:
"why not handle it in the same way that C++ widget sets handle it"
since its essentially the same problem, viz. call a particular
method on some class that the caller has no idea about.

--linas



More information about the Gsl-discuss mailing list