Jakub Klinkovský
Jakub Klinkovský

Reputation: 1362

clang++ 8.0.1 self-assign-overloaded warnings

Consider this code:

class Vector {
public:
    Vector& operator+=(const Vector &v) { return *this; }
    Vector& operator-=(const Vector &v) { return *this; }
    Vector& operator*=(const Vector &v) { return *this; }
    Vector& operator/=(const Vector &v) { return *this; }
};

int main()
{
    Vector v;
    v += v;
    v -= v;
    v *= v;
    v /= v;
}

When compiled with clang++ 8.0.1, I get the following warnings:

$ clang++ -Wall example2.cpp -o example2
example2.cpp:13:7: warning: explicitly assigning value of variable of type 'Vector' to
      itself [-Wself-assign-overloaded]
    v -= v;
    ~ ^  ~
example2.cpp:15:7: warning: explicitly assigning value of variable of type 'Vector' to
      itself [-Wself-assign-overloaded]
    v /= v;
    ~ ^  ~
2 warnings generated.

What problem does the warning try to point out? Why is there no such warning for operator+= and operator*=?

Edit: For a motivation (or real-world usage of such self-assign expressions) see https://github.com/pybind/pybind11/issues/1893

Edit 2: I simplified the code by removing everything but the return statements from the operators; the same problem still occurs.

Upvotes: 7

Views: 2924

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96924

Here's my guess:

Clang recognizes that for scalars v -= v means v = 0.

It assumes that every time you use v -= v, it's a fancy way of saying v = 0.

But since v is a class with overloaded -=, it warns you that in this case v -= v might not be equivalent to v = 0.

It's does the same thing for v /= v, because it's equivalent to v = 1 for scalars (unless v == 0).


If I got the reasoning corrent, this is a pretty stupid warning, IMO.

I'd rather warn on v -= v; and v /= v; being used on scalars.

Upvotes: 3

Related Questions