[Haskell-cafe] C++ interface with Haskell

Evan Laforge qdunkan at gmail.com
Fri Apr 18 15:08:10 EDT 2008


To threadjack a little bit, I've been interfacing haskell with c++.
It gets awkward when the c++ structures use STL types like string and
vector.  Of course those are too complex for haskell to marshal to.

What I've been doing is defining an XMarshal variant of the X c++
class, that uses plain c arrays.  Then I marshal to that, and
construct the c++ object properly from XMarshal in the c->c++ wrapper
layer.  On a few occasions, when the c++ class is really big and only
has one STL member, I make a partially constructed c++ object, pass
the array separately, and then construct the proper c++ class from the
broken haskell generated one.  Possibly dangerous as all get-out
because I'm dealing with "unconstructed" c++ objects, but it seems to
work.

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.

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?


I've also noticed warnings from g++ about hsc2hs's use of the OFFSETOF
macro on c++ classes, but some googling of g++ mailing lists implied
that it's harmless if you don't have virtual bases, and what sane
person does, so I suppress it now :)


More information about the Haskell-Cafe mailing list