bool image_manager::contains_image(const std::string& filename)
{
return this->map_.count(filename);
}
现在我得到的警告是:
warning C4800: 'unsigned int' : forcing value to bool 'true' or 'false' (performance warning)
但是由于 std::map
的 count() 方法的返回类型是:
1 if an element with a key equivalent to x is found, or zero otherwise.
因此它可以像 boolean 值一样使用。那么为什么我会收到这个警告呢?在 C++ 中,整数基本上可以用于 boolean 检查,对吧?因此 0 == false
和 1 == true
。那么为什么编译器会给我一个警告呢?我也试过像这样使用 static_cast
:
return static_cast<bool>(this->map_.count(filename));
但我仍然收到警告。
最佳答案
一般来说,unsigned int
不是 bool
,因此会出现警告。尝试:
return this->map_.count(filename) > 0;
相反。
关于c++ - 为什么我会在 MSVC 上收到此警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702824/