枚举静态转换为 bool,来自编译器的性能警告
Static cast of enum to bool, performance warning from Compiler
我在我的项目中声明了以下内容:
enum class OType : bool { Dynamic=true, Static=false };
OType getotype();
我正在使用以下功能:
double ComputeO(double K,bool type)
我是这样称呼它的:
ComputeO(some double, static_cast<bool>(getotype()))
为此static_cast我很高兴:
warning C4800: 'const dmodel::OType ' : forcing value to bool 'true' or 'false' (performance warning)
我不知道如何摆脱它,我明确指定了演员表应该不够吗?
注意:我使用的是 VC11(Visual Studio 2012)
谢谢。
请参阅 https://msdn.microsoft.com/en-us/library/b6801kcy.aspx,其中描述了警告。特别是,它说:
Casting the expression to type bool will not disable the warning,
which is by design.
只需像这样重写您的调用:
enum class OType : bool { Dynamic=true, Static=false };
OType getotype();
double ComputeO(double K,bool type);
int main()
{
ComputeO(1.0, getotype() == OType::Dynamic);
}
我在我的项目中声明了以下内容:
enum class OType : bool { Dynamic=true, Static=false };
OType getotype();
我正在使用以下功能:
double ComputeO(double K,bool type)
我是这样称呼它的:
ComputeO(some double, static_cast<bool>(getotype()))
为此static_cast我很高兴:
warning C4800: 'const dmodel::OType ' : forcing value to bool 'true' or 'false' (performance warning)
我不知道如何摆脱它,我明确指定了演员表应该不够吗?
注意:我使用的是 VC11(Visual Studio 2012)
谢谢。
请参阅 https://msdn.microsoft.com/en-us/library/b6801kcy.aspx,其中描述了警告。特别是,它说:
Casting the expression to type bool will not disable the warning, which is by design.
只需像这样重写您的调用:
enum class OType : bool { Dynamic=true, Static=false };
OType getotype();
double ComputeO(double K,bool type);
int main()
{
ComputeO(1.0, getotype() == OType::Dynamic);
}