Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Show edge case with operator overloading, reversal, and mixing #2513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ throwing a type error.

The file :file:`tests/test_operator_overloading.cpp` contains a
complete example that demonstrates how to work with overloaded operators in
more detail.
more detail, as well of some of the nuances you may encounter.

.. _pickling:

Expand Down
21 changes: 21 additions & 0 deletions tests/test_operator_overloading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ int operator+(const C2 &, const C2 &) { return 22; }
int operator+(const C2 &, const C1 &) { return 21; }
int operator+(const C1 &, const C2 &) { return 12; }



// Note: Specializing explicit within `namespace std { ... }` is done due to a
// bug in GCC<7. If you are supporting compilers later than this, consider
// specializing `using template<> struct std::hash<...>` in the global
Expand All @@ -80,6 +82,17 @@ std::string abs(const Vector2&) {
return "abs(Vector2)";
}

// ReverseA and ReverseB simulate two classes defined in separate files, both
// with oeprator overloads on +, and ReverseB wishes to overload __radd__ with
// ReversedA. This won't work, as will be shown in the Python test.
struct ReverseA {
friend int operator+(int, ReverseA) { return 1; };
};
struct ReverseB {
friend int operator+(ReverseB, ReverseA) { return 2; };
friend int operator+(ReverseA, ReverseB) { return 3; };
};

// MSVC warns about unknown pragmas, and warnings are errors.
#ifndef _MSC_VER
#pragma GCC diagnostic push
Expand Down Expand Up @@ -219,6 +232,14 @@ TEST_SUBMODULE(operators, m) {
.def("__hash__", &Hashable::hash)
.def(py::init<int>())
.def(py::self == py::self);

py::class_<ReverseA>(m, "ReverseA")
.def(py::init())
.def(int{} + py::self);
py::class_<ReverseB>(m, "ReverseB")
.def(py::init())
.def(py::self + ReverseA{})
.def(ReverseA{} + py::self);
}

#ifndef _MSC_VER
Expand Down
6 changes: 6 additions & 0 deletions tests/test_operator_overloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,9 @@ def test_overriding_eq_reset_hash():

assert hash(hashable(15)) == 15
assert hash(hashable(15)) == hash(hashable(15))


def test_reverse_operator_ambiguity():
assert int() + m.ReverseA() == 1
assert m.ReverseB() + m.ReverseA() == 2
assert m.ReverseA() + m.ReverseB() == 3