c++ - 警告 C4800 : 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

标签 c++ visual-studio visual-c++

当我在 Visual Studio 2008 中编译以下代码片段时,我收到了这个警告。

BOOL
CPlan::getStandardPlan() const
{
    return m_standardPlan;
}


bool m_bStandardPlan;

if(plan!=NULL)
{
    // Assign the values to the Cola object
    poCola->m_lPlanId           = plan->getPlanId();
    poCola->m_lPlanElementId        = plan->getPlanElementId();
    poCola->m_lPlanElementBaseId        = plan->getPlanElementBaseId();
    poCola->m_bStandardPlan         = plan->getStandardPlan(); //C4800

    return 1;
}

我引用了以下链接,

http://msdn.microsoft.com/en-us/library/b6801kcy%28v=vs.90%29.aspx

Forcing value to boolean: (bool) makes warning, !! doesnt

Warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

我不确定如何解决此警告。

最佳答案

BOOL 是 WinAPI 中某处 int 的 typedef。 WinAPI 是一个 C API,所以他们不能使用 C++ 的 bool。如果你不能通过从函数返回一个 bool 来摆脱它,例如因为您不维护该功能,所以您可以使用对零的显式检查来消除警告:

poCola->m_bStandardPlan = (plan->getStandardPlan() != 0);

另一个考虑是添加一个封装检查的函数:

bool getStandardPlan(CPlan const& plan) {
  return plan->getStandardPlan() != 0;
}

然后

poCola->m_bStandardPlan = getStandardPlan(plan);

关于c++ - 警告 C4800 : 'BOOL' : forcing value to bool 'true' or 'false' (performance warning),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22498767/

相关文章:

c++ - 没有合适的用户定义转换

c++ - 将 Rect 转换为 vector<Point>

c++ - 你知道学习运算符优先级和结合性的心理装置或技巧吗?

c# - 如何检查鼠标移动事件中的键盘键状态

visual-studio - 如何防止 Visual Studio 在 Debug模式下切换视角?

c++ - MinGW/GCC 延迟加载 DLL 等价物?

c++ - #elseif 与 #elif(C/C++ 预处理器)

c# - 发生异常后向后单步执行程序 - Visual Studio

c++ - getline() 没有填充整个字符串数组

c++ - std::ostringstream 作为函数参数的问题