αλε*_*λυτ 5 c++ compiler-warnings compiler-options temporary-objects
当您尝试分配给临时对象时,是否有任何编译器选项允许您收到警告?
例子:
struct S {
S op() { return S(); }
};
int main() {
S s;
s.op() = s; // assign to temporary. Wants to warn here.
}
Run Code Online (Sandbox Code Playgroud)
我知道你可以声明opas的返回类型const来防止这种情况,但现在我感兴趣的只是编译器选项。
您可以使用任何流行的现代编译器。
编译器可能无法识别有用的副作用。
编译器会发出警告
int test( S & data );
test( S.op());
Run Code Online (Sandbox Code Playgroud)