如何解决打开-Weffc++ 时的"user-defined 'operator||()' always evaluates both arguments" 警告?
How to resolve "user-defined 'operator||()' always evaluates both arguments" warning when -Weffc++ is turned on?
这是一个重载 ||在我的 class:
中定义的运算符
bool operator|| (const MyClass& v) const {
return ......; //some calculation
}
编译器报警告:
warning: user-defined 'bool MyClass::operator||(const MyClass&) const'
always evaluates both arguments [-Weffc++]
我理解警告,因为内置 ||是短路的,这可能与用户定义的运算符打算表现的不同。但问题是,我需要打开 -Weffc++
并且不允许任何警告。那么||的代码是什么重载可以解决此警告(即抑制此警告)?谢谢。
我在 Ubuntu 16.04 上使用 g++ 5.4.0。
您可以通过不重载逻辑运算符(其内置版本短路)来避免警告。如果您应该遵循 -Weffc++
选项的指导方针,那么您应该不声明此类重载。
您可以使用:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
// the declaration
#pragma GCC diagnostic pop
无论编译选项如何,暂时抑制警告。
这是一个重载 ||在我的 class:
中定义的运算符bool operator|| (const MyClass& v) const {
return ......; //some calculation
}
编译器报警告:
warning: user-defined 'bool MyClass::operator||(const MyClass&) const' always evaluates both arguments [-Weffc++]
我理解警告,因为内置 ||是短路的,这可能与用户定义的运算符打算表现的不同。但问题是,我需要打开 -Weffc++
并且不允许任何警告。那么||的代码是什么重载可以解决此警告(即抑制此警告)?谢谢。
我在 Ubuntu 16.04 上使用 g++ 5.4.0。
您可以通过不重载逻辑运算符(其内置版本短路)来避免警告。如果您应该遵循 -Weffc++
选项的指导方针,那么您应该不声明此类重载。
您可以使用:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
// the declaration
#pragma GCC diagnostic pop
无论编译选项如何,暂时抑制警告。