12a13
> #include <functional>
18a20,21
> Vector2 &operator=(const Vector2 &v) { x = v.x; y = v.y; print_copy_assigned(this); return *this; }
> Vector2 &operator=(Vector2 &&v) { x = v.x; y = v.y; v.x = v.y = 0; print_move_assigned(this); return *this; }
21,23c24
< std::string toString() const {
< return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
< }
---
> std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; }
25,35d25
< void operator=(const Vector2 &v) {
< print_copy_assigned(this);
< x = v.x;
< y = v.y;
< }
<
< void operator=(Vector2 &&v) {
< print_move_assigned(this);
< x = v.x; y = v.y; v.x = v.y = 0;
< }
<
41a32,33
> Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
> Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
45a38,39
> Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
> Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
55c49,67
< test_initializer operator_overloading([](py::module &m) {
---
> class C1 { };
> class C2 { };
>
> int operator+(const C1 &, const C1 &) { return 11; }
> int operator+(const C2 &, const C2 &) { return 22; }
> int operator+(const C2 &, const C1 &) { return 21; }
> int operator+(const C1 &, const C2 &) { return 12; }
>
> namespace std {
> template<>
> struct hash<Vector2> {
> // Not a good hash function, but easy to test
> size_t operator()(const Vector2 &) { return 4; }
> };
> }
>
> TEST_SUBMODULE(operators, m) {
>
> // test_operator_overloading
63a76,77
> .def(py::self * py::self)
> .def(py::self / py::self)
67a82,83
> .def(py::self *= py::self)
> .def(py::self /= py::self)
72a89
> .def(hash(py::self))
76c93,146
< });
---
>
> // test_operators_notimplemented
> // #393: need to return NotSupported to ensure correct arithmetic operator behavior
> py::class_<C1>(m, "C1")
> .def(py::init<>())
> .def(py::self + py::self);
>
> py::class_<C2>(m, "C2")
> .def(py::init<>())
> .def(py::self + py::self)
> .def("__add__", [](const C2& c2, const C1& c1) { return c2 + c1; })
> .def("__radd__", [](const C2& c2, const C1& c1) { return c1 + c2; });
>
> // test_nested
> // #328: first member in a class can't be used in operators
> struct NestABase { int value = -2; };
> py::class_<NestABase>(m, "NestABase")
> .def(py::init<>())
> .def_readwrite("value", &NestABase::value);
>
> struct NestA : NestABase {
> int value = 3;
> NestA& operator+=(int i) { value += i; return *this; }
> };
> py::class_<NestA>(m, "NestA")
> .def(py::init<>())
> .def(py::self += int())
> .def("as_base", [](NestA &a) -> NestABase& {
> return (NestABase&) a;
> }, py::return_value_policy::reference_internal);
> m.def("get_NestA", [](const NestA &a) { return a.value; });
>
> struct NestB {
> NestA a;
> int value = 4;
> NestB& operator-=(int i) { value -= i; return *this; }
> };
> py::class_<NestB>(m, "NestB")
> .def(py::init<>())
> .def(py::self -= int())
> .def_readwrite("a", &NestB::a);
> m.def("get_NestB", [](const NestB &b) { return b.value; });
>
> struct NestC {
> NestB b;
> int value = 5;
> NestC& operator*=(int i) { value *= i; return *this; }
> };
> py::class_<NestC>(m, "NestC")
> .def(py::init<>())
> .def(py::self *= int())
> .def_readwrite("b", &NestC::b);
> m.def("get_NestC", [](const NestC &c) { return c.value; });
> }