[C++-sig] Some questions ...

Mike Thompson mike.thompson at day8.com.au
Tue Sep 9 06:33:29 CEST 2003

First, Boost.Python is a delight.

It's wonderfully functional, bug free and has quite reasonable documentation.
After a few initall issues with bjam and stlport, it's all working very, very
well for me.

My C++ library accesses objects in memory mapped databases, so I was quite
worried about technical challenges I might face in the python <-> C++
interface, but its been a breeze (so far) with Boost.Python.   I now have
python based unit tests for my library which, to me, is so much easier than
having C++ based test harnesses.  That alone is a huge bonus.

The main game for me soon though is building a wxPython application using this
newly wrapped library.

Based on my learning experience so far, I'd make the following simple

1.   add a link to the Boost.Python Wiki in
      I only found the Wiki once I started googling for answers but it would
      been better for me if I'd seen it initially.  It's pages answered some of

2.   On the same page you might also want to mention that
      contains unittests that newbies might find interesting.  I certainly
found them a
      useful set of examples when once or twice I was stuck.

3.   At the very bottom of:

      there's a "notes box"  which starts
          "What is the business of operator<< .def(str(self))?"
      that's got to be a mistake, I think, because I can find no mention
      of operator<< .def(str(self)) on that page.  The rest of
   the note, after that first sentence, is fine.

So far so good, but now I have a couple of questions.

Q1.   It's possible to 'graft' member functions onto a class ... like this:

          class X  {   // ...  };

          void myMethod(const X& x) {   // ... }

                  // ...
                  .def("meth", &myMethod)

         BUT is it possible to graft new constructors onto a class?

         I realise I could create a derived class from the one I'm wrapping and
         add the extra constructor there (as well as possibily replicating the
         existing ones), but are there any other suggestions?

Q2.  The following function works, but I'm left wondering if there's not a
         better way.  It takes a list of ints and returns the equivalent

         vector<int>  makeVectorFromList(list l)
             boost::python::object olen = l.attr("__len__")();
             int len = extract<int>(olen)();

             vector<int> ret(len);

             for (int i = 0; i < len; ++i) {
                 extract<int>  get_int(l[i]);
                 if (!get_int.check()) {
                     PyErr_SetString(PyExc_TypeError, "makeVectorFromList()
expects a list of ints");
                     throw error_already_set();
                 ret[i] = get_int();
            return ret;


More information about the Cplusplus-sig mailing list