【发布时间】:2019-08-25 12:06:32
【问题描述】:
考虑这段代码:
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;
}
使用 clang++ 8.0.1 编译时,我收到以下警告:
$ 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.
警告试图指出什么问题?为什么operator+=和operator*=没有这样的警告?
编辑:有关动机(或此类自赋值表达式的实际用法),请参阅https://github.com/pybind/pybind11/issues/1893
编辑 2: 我简化了代码,删除了运算符中除了 return 语句之外的所有内容;同样的问题仍然存在。
【问题讨论】:
-
我报告了一个clang的错误:bugs.llvm.org/show_bug.cgi?id=43124
标签: c++ compiler-warnings clang++