在Windows上编译时,编译器会发出以下警告:
forcing value to bool 'true' or 'false' (performance warning)
当我做类似的事情时就会出现:
int a = ...
bool b = (a & (1 << 3);
解决方案是:
bool b = (a & (1 << 3)) != 0;
或使用int代替bool。
问题是:为什么第一种情况会引起性能问题,而第二种情况不会引起性能问题?另外,为什么我没有警告:
if (a & (1 << 3)) {
...
}
因为在这种情况下,该值会转换为bool,不是吗?
最佳答案
此警告是在某些情况下使用不正确的单词拼写的过时的Visual Studio 2015编译器。现在听起来更正确
Implicit conversion from int to bool. Possible information loss
Compiler Warning (level 4) C4800
关于c++ - 为什么强制int夸大性能问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61511382/