escaping addresses of ref parameters - not

Denis Koroskin 2korden at gmail.com
Mon Feb 9 04:53:06 PST 2009


Christopher Wright Wrote:

> Daniel Keep wrote:
> > I've used ref arguments in the past to wrap a C api that expects
> > pointers.  I'm fine with this so long as there is a way to break out of
> > it (in regular D, at least) that makes it abundantly clear you need to
> > know what you're doing.
> > 
> > Something like:
> > 
> > void wrapSomeCApi(ref Foo arg)
> > {
> >     Foo* argptr = ref_unsafe_escape(arg);
> >     some_c_api(argptr);
> > }
> > 
> > Incidentally, I don't suppose we can get ref variables while Walter's at
> > it? :P
> > 
> >   -- Daniel
> 
> Why aren't you passing a Foo*?

That's ok if Foo is a struct:

struct Rect
{
    int x, y, width, weight;
};

class Widget {
  // returns true on success
  bool getBounds(ref Rect rect) {
    Rect* rectPtr = ref_unsafe_escape(rect);
    return gtk_widget_get_bounds(rectPtr) != 0;
  }
}

Another question is, what is the benefit of it? Why not take an adress directly?

return gtk_widget_get_bounds(&rect) != 0;



More information about the Digitalmars-d mailing list