From 73a50a04410db0168af937152793d7f107e18f10 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Fri, 11 Sep 2015 17:09:47 +0200 Subject: [PATCH] Fix for radd/rsub/rmul/rdiv operator convenience wrappers --- example/example3.cpp | 10 ++++++++++ example/example3.py | 4 ++++ include/pybind/operators.h | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/example/example3.cpp b/example/example3.cpp index 089f219cc..80bdf0e43 100644 --- a/example/example3.cpp +++ b/example/example3.cpp @@ -42,10 +42,16 @@ public: Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; } Vector2& operator*=(float v) { x *= v; y *= v; return *this; } Vector2& operator/=(float v) { x /= v; y /= v; return *this; } + + friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); } + friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); } + friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); } + friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); } private: float x, y; }; + void init_ex3(py::module &m) { py::class_(m, "Vector2") .def(py::init()) @@ -59,6 +65,10 @@ void init_ex3(py::module &m) { .def(py::self -= py::self) .def(py::self *= float()) .def(py::self /= float()) + .def(float() + py::self) + .def(float() - py::self) + .def(float() * py::self) + .def(float() / py::self) .def("__str__", &Vector2::toString); m.attr("Vector") = m.attr("Vector2"); diff --git a/example/example3.py b/example/example3.py index 41cf31441..591ae391c 100755 --- a/example/example3.py +++ b/example/example3.py @@ -16,6 +16,10 @@ print("v1-8 = " + str(v1-8)) print("v1+8 = " + str(v1+8)) print("v1*8 = " + str(v1*8)) print("v1/8 = " + str(v1/8)) +print("8-v1 = " + str(8-v1)) +print("8+v1 = " + str(8+v1)) +print("8*v1 = " + str(8*v1)) +print("8/v1 = " + str(8/v1)) v1 += v2 v1 *= 2 diff --git a/include/pybind/operators.h b/include/pybind/operators.h index 84fc4c250..1e08fdac4 100644 --- a/include/pybind/operators.h +++ b/include/pybind/operators.h @@ -67,8 +67,8 @@ template struct op_impl struct op_impl { \ static char const* name() { return "__" #rid "__"; } \ - static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \ - static B execute_cast(const L &l, const R &r) { return B(expr); } \ + static auto execute(const R &r, const L &l) -> decltype(expr) { return (expr); } \ + static B execute_cast(const R &r, const L &l) { return B(expr); } \ }; \ inline op_ op(const self_t &, const self_t &) { \ return op_(); \