[Haskell-cafe] C++ interface with Haskell

Isaac Dupree isaacdupree at charter.net
Sat Apr 19 13:27:44 EDT 2008

you could write a C++ function to marshal a Sequence (or any Container 
IIRC, maybe Forward Container) to a vector (or whatever you wanted -- 
there are choices), and then

okay let's see if I remember C++ well enough

This design has extra copying. but anyway

template<typename Container>
std::vector<typename Container::value_type> 
container_to_vector(Container const& c) {
   return std::vector<typename Container::value_type>(c.begin(), c.end());


template<typename Container>
Container vector_to_sequence(std::vector<typename Container::value_type> 
const& c) {
   return Container(c.begin(), c.end());

extern "C" {
/* the temporary returned variable doesn't last long enough here */
(char*, int)/*I know C++ doesn't have this syntax of tuples*/ 
string_to_array(std::string const& s) {
   return (&*container_to_vector(s).begin())

In other words I suspect that it's possible with a minimum of 
boilerplate per type, (possibly including the use of macros), but I'm 
not sure exactly what you need to do, and I got tired of being a C++-fu 
expert a few years ago

More information about the Haskell-Cafe mailing list