Detecting (meaningless) assignment to a temporary object

Is there a compiler option to get a warning when trying to assign to a temporary object?

Example:

struct S {
    S op() { return S(); }
};

int main() {
    S s;
    s.op() = s; // assign to temporary. Wants to warn here.
}

I know that you can declare a return type opas constto prevent such a situation, but now I'm only interested in compiler options.

You can use any popular modern compiler.

+4
source share
1 answer

The compiler may not know the beneficial side effects.

Compilers warn about

int test( S & data );

test( S.op());
0
source

Source: https://habr.com/ru/post/1611912/


All Articles