Boost logo

Boost :

From: Peter.Bienstman_at_[hidden]
Date: 2001-07-19 04:07:58


> That's right. Boost.Python was developed just before in-place
operators
> were introduced into Python, so they are currently unsopported. I'll
> look into that as soon as I have time. Still, I'd have guessed that
your
> approach should have worked. What's the problem?

Boost insists on treating __iadd__ differently than other functions:

#include <iostream>
#include "boost/python/class_builder.hpp"

using namespace boost::python;

class Term
{
  public:
    Term() {}
};

class Expression
{
  public:
    Expression() {}
    Expression operator +=(const Term&)
      {std::cout << "operator +=" << std::endl;}
};

BOOST_PYTHON_MODULE_INIT(inplace)
{
  try
  {
    
  module_builder inplace("inplace");

  class_builder<Term> Term_(inplace, "Term");
  Term_.def(constructor<>());
  
  class_builder<Expression> Expression_(inplace, "Expression");
  Expression_.def(constructor<>());
  Expression_.def(&Expression::operator+=, "add"); // this works
  Expression_.def(&Expression::operator+=, "__iadd__"); // this not
  }
  catch(...)
  {
  }
  
}

Python 2.1 (#1, Jun 29 2001, 13:45:42)
[GCC 3.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from inplace import *
>>> a = Term()
>>> b = Expression()
>>> b.add(a)
operator +=
<Expression object at 0x812caa0>
>>> b += a
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +=


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk