[C++-sig] Re: wrapping constructor

Neal D. Becker ndbecker2 at verizon.net
Thu Apr 8 14:56:21 CEST 2004


Thanks, this is very helpful.  Now a related question.

I'm trying to wrap lots of C++ code, which is all written for STL-sytle
iterators.

So, a class X would look like:

class X {
  template<typename it_t> X (it_t beg, it_t end) {...}
  template<typename in_t, typename out_t> void Compute (in_t in, in_t inend,
out_t out) {...}
};

I can write a non-member wrapper function to convert from a python container
to the iterator interface needed by the constructor, as you described.  How
do I handle the member function?  Can I make a non-member C++ wrapper, and
then inject this into the class?  How?

template<typename CONT>
inline X* MakeX (CONT v) {
  return new X (v.begin(), v.end());
}

template<typename CONT>
inline void Compute_wrap (X& c, CONT v) {
  c.Compute (v.begin(), v.end());
}

BOOST_PYTHON_MODULE(Corr4_wrap) {
  typedef std::vector<Complex>::const_iterator vc_cit;

  class_<X> ("X", no_init)
    .def("__init__", boost::python::make_constructor(MakeX<const
std::vector<Complex>& >))
    .def("Compute", <what goes here?> )
    ;

}






More information about the Cplusplus-sig mailing list