枚举静态转换为布尔,编译器发出性能警告



我在我的项目中声明了以下内容:

enum class OType : bool { Dynamic=true, Static=false };
OType getotype();

我正在使用以下功能:

double ComputeO(double K,bool type)

我这样称呼它:

ComputeO(some double, static_cast<bool>(getotype()))

对于这个静态广播,我得到了一个不错的:

warning C4800: 'const dmodel::OType ' : forcing value to bool 'true' or 'false' (performance warning)

我不知道如何摆脱它,我明确指定了演员阵容,难道这还不够吗?

注意:我使用的是VC11(Visual Studio 2012)

Thks。

请参阅https://msdn.microsoft.com/en-us/library/b6801kcy.aspx,描述了警告。特别是,它说:

将表达式强制转换为bool类型不会禁用警告,这是经过设计的。

只需像这样重写您的呼叫:

enum class OType : bool { Dynamic=true, Static=false };
OType getotype();
double ComputeO(double K,bool type);
int main()
{
    ComputeO(1.0, getotype() == OType::Dynamic);
}

最新更新