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

半腔热情 提交于 2019-12-22 01:33:44

问题


I have this problem in my code:

bool CBase::isNumber()
{
return (id & MID_NUMBER);
}

bool CBase::isVar()
{
return (id & MID_VARIABLE);
}

bool CBase::isSymbol()
{
return (id & MID_SYMBOL);
}

回答1:


FYI: Casts won't hide the warning by design.

Something like

return (id & MID_NUMBER) != 0;

should clearly state "I want to check whether this value is zero or not" and let the compiler be happy




回答2:


Use the !! idiom eg

bool CBase::isNumber()
{
    return !!(id & MID_NUMBER);
}



回答3:


Where's the declaration of id and MID_NUMBER? Are you sure they are not windef-style BOOLs rather than (lowercase) bool's? BOOL's have been in windef for decades typedef'd as an int; they pre-date proper C++ bool's and a lot of developers still use them.



来源:https://stackoverflow.com/questions/20919650/warning-c4800-int-forcing-value-to-bool-true-or-false-performance-warn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!