user5280911
user5280911

Reputation: 763

How to resolve "user-defined 'operator||()' always evaluates both arguments" warning when -Weffc++ is turned on?

This is an overloaded || operator defined in my class:

bool operator|| (const MyClass& v) const {
    return ......;  //some calculation
}

The compiler reports an warning:

warning: user-defined 'bool MyClass::operator||(const MyClass&) const' always evaluates both arguments [-Weffc++]

I understand the warning because built-in || is short-circuit which might be different from what the user-defined operator intends to behave. But the thing is, I am required to have -Weffc++ turned on and any warning is not allowed. So what code of || overloading can resolve this warning (i.e., suppress this warning)? Thank you.

I'm using g++ 5.4.0 on Ubuntu 16.04.

Upvotes: 0

Views: 272

Answers (1)

eerorika
eerorika

Reputation: 238351

You can avoid the warning by not overloading the logical operators (whose built-in versions short-circuit). If you're supposed to follow the guidelines of the -Weffc++ option, then you're supposed to not declare such overloads.

You can use:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
// the declaration
#pragma GCC diagnostic pop

To temporarily suppress the warning regardless of compilation options.

Upvotes: 2

Related Questions