Consider the following code (slightly modified version of the Operator overloading example):
#include <string>
#include <pybind11/operators.h>
namespace py = pybind11;
class Vector2 {
public:
Vector2(float x, float y) : x(x), y(y) { }
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; }
private:
float x, y;
};
PYBIND11_MODULE(example, m) {
py::class_<Vector2>(m, "Vector2")
.def(py::init<float, float>())
.def(py::self += py::self)
.def(py::self -= py::self);
}
When compiled with Clang 8.0.1 (on Arch Linux), this leads to the following warning:
$ clang++ -Wall example.cpp -o example.so -shared -fPIC -I ~/.local/include/ -I /usr/include/python3.7m/ -lpython3.7m
example.cpp:20:23: warning: explicitly assigning value of variable of type 'const
pybind11::detail::self_t' to itself [-Wself-assign-overloaded]
.def(py::self -= py::self);
~~~~~~~~ ^ ~~~~~~~~
1 warning generated.
Strangely enough, there is no warning for the operator+= overload. The same warning happens also for operator/=, but not for operator*=.
What shall we do about this (since libraries should not trigger compiler warnings)?
Actually, the problem is reproducible even without pybind11: https://stackoverflow.com/questions/57645872/clang-8-0-1-self-assign-overloaded-warnings
If it helps you, this is what I ended up doing to silence those: https://github.com/mosra/magnum-bindings/blob/1ec72697322715ce1d79cdd2eb22f11c1a6fc0af/src/python/magnum/math.vector.h#L99-L106
Also desperately looking for a better solution.
This should probably be escalated to upstream (i.e. Clang bugtracker), the warning is overzealous and will cause issues in meta-template frameworks.
Reported as https://bugs.llvm.org/show_bug.cgi?id=43124
Most helpful comment
Reported as https://bugs.llvm.org/show_bug.cgi?id=43124