[Haskell-cafe] C++ interface with Haskell

Evan Laforge qdunkan at gmail.com
Sat Apr 19 12:30:43 EDT 2008

>  you mean, you hack around with the internal representation of those
> structures?  Well, if you want to avoid double-copying, C++ can't access
> Haskell sequences, and Haskell can't access C++ sequences, I guess I don't
> see an alternative.

I don't really mind double copying, but having to declare c variants
(i.e. no stl) of all the c++ structures and then copy from one to the
other is a little tedious.  I was hoping there was some clever trick
to make that easier...

> > Passing back to haskell is easier since I can use "&*vec.begin()",
> > which according to the internet should be safe because STL guarantees
> > that vector contents are contiguous.
> >
>  safe until either:
>  the vector's contents change, if Haskell is assuming it's immutable,

Well, naturally I peekArray before letting c++ have it back.

> > I'm only saved by the fact that I don't have that many different kinds
> > of classes to pass.  This would be much more drudgery if I had more.
> > Does anyone have a better solution or convention for marshalling c++
> > objects?
> >
>  not "better", but, you could wrap the methods of the class and call back
> into C++ (through C wrappers) to do anything with the class, if it suited
> your purposes better and wasn't too slow

Yeah, I was thinking of that, but it seemed like even more of a
hassle.  However, a more practical variant might be to write a haskell
interface to marshal to vectors and strings, probably involving some
void pointer sketchiness since naturally template types can't be
expressed in a C function signature, and then poke those into the
struct when I pass it.  That would only work for vectors stored by
pointer of course.

More information about the Haskell-Cafe mailing list