[C++-sig] Boost.Python v2: automatic operator wrapping support

David Abrahams david.abrahams at rcn.com
Sun Jun 2 20:57:51 CEST 2002


I have implemented automatic wrapping of C++ operators for Boost.Python v2
in the current CVS state. Documentation to follow.

Quick summary:

#include <boost/python/operators.hpp>

In a class_<> definition, use the special object boost::python::self to
expose C++ operators:

    .def(self < self)
    .def(self == self)
    .def(self += self)

Heterogeneous operators can be defined as follows:

    .def(self * int())
    .def(int() * self)
or

    .def(self * 1)
    .def(1 * self)

or, if the other type is expensive to create,

    .def(self * other<my_heavy_type>())
    .def(other<my_heavy_type>() * self)

Support for Python's __int__ __float__ and __complex__ are provided:

    .def(int_(self))    // uses (long)x
    .def(float_(self))  // uses (double)x
    .def(complex_(self) // uses std::complex<double>(x)

Support for __str__ is provided:

    .def(str(self)) // uses lexical_cast<std::string>(x)

Support for __pow__(x,y) is provided:

    .def(pow(self,int())

Enjoy,
Dave

+---------------------------------------------------------------+
                  David Abrahams
      C++ Booster (http://www.boost.org)               O__  ==
      Pythonista (http://www.python.org)              c/ /'_ ==
  resume: http://users.rcn.com/abrahams/resume.html  (*) \(*) ==
          email: david.abrahams at rcn.com
+---------------------------------------------------------------+






More information about the Cplusplus-sig mailing list