RosettaCommons / binder

Binder, tool for automatic generation of Python bindings
MIT License
322 stars 66 forks source link

Unary `+`, `-`, `*` invalid conversion #225

Closed zwimer closed 2 years ago

zwimer commented 2 years ago

The unary + and - operators are treated as __add__ and __sub__ instead of __pos__ and __neg__.

For example, calling binder on:

namespace Test {
  struct A {
    int a;
    inline A operator+() const { return *this; }
    inline A operator-() const { return {-this->a}; }
  };
}

Yields:

{
    pybind11::class_<Test::A, std::shared_ptr<Test::A>> cl(M("Test"), "A", "" );
    cl.def( pybind11::init( [](){ return new Test::A(); } ) );
    cl.def( pybind11::init( [](Test::A const &o){ return new Test::A(o); } ) );
    cl.def_readwrite("a", &Test::A::a);
    cl.def("val", (int (Test::A::*)() const) &Test::A::val, "C++: Test::A::val() const --> int");
    cl.def("__add__", (struct Test::A (Test::A::*)() const) &Test::A::operator+, "C++: Test::A::operator+() const --> struct Test::A");
    cl.def("__sub__", (struct Test::A (Test::A::*)() const) &Test::A::operator-, "C++: Test::A::operator-() const --> struct 
}

Also the unary * operator (dereference) is converted to __mul__.

lyskov commented 2 years ago

Thank you for reporting this @zwimer ! This should be fixable. - are you planing to submit PR with fix? If not then i will add this to my list. Please let me know to avoid effort duplication. Thanks,

zwimer commented 2 years ago

I only documented this. For my use cases I just ran through every operator to determine what was usable and now then made a PR with those results linked above. I don't currently plan to fix these.

zwimer commented 2 years ago

@lyskov I ended up needing these so I fixed them and made a PR

zwimer commented 2 years ago

The PR: https://github.com/RosettaCommons/binder/pull/237