如何避免 VS 中unique_ptr签入的性能警告
How to avoid performance warning for unique_ptr check in VS?
此代码:
unique_ptr<int> a;
if (a) {
cout << "ASSIGNED" << endl;
}
甚至这个代码:
unique_ptr<int> a;
if (static_cast<bool>(a)) {
cout << "ASSIGNED" << endl;
}
导致此警告:
warning C4800: 'void (__cdecl *)(std::_Bool_struct<_Ty> &)' : forcing value to bool 'true' or 'false' (performance warning)
with
[
_Ty=std::unique_ptr<int>
]
在 Visual Studio 2012 中的警告级别为 3。在第一条评论之后,我发现它仅在打开公共语言运行时支持/clr 时才会发生。我应该如何避免它?
if (a.get() != nullptr)
应该有效,但我认为这不是unique_ptr的设计方式,是吗?
您可以
直接使用
if (a != nullptr)
通常可以使用!!
清除Visual Studio上的性能警告:
unique_ptr<int> a;
if (!!a) {
cout << "ASSIGNED" << endl;
}
我很久以前在迈克尔霍华德的博客上读到过它,但我没有参考。他谈论的是干净的编译和使用编译器,而不是关闭警告。
相关文章:
- 警告处理为错误这里有什么问题
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- OpenMP阵列性能较差
- 递归列出所有目录中的C++与Python与Ruby的性能
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- cppcheck在const std::string[]上引发警告
- GCC对可能有效的代码抛出init list生存期警告
- 如何在BST的这个简单递归实现中消除警告
- 大小相等但成员数量不同的结构之间的性能差异
- 关于std::move的使用,是否有编译警告
- g++ 在某个类成员未初始化时不发出警告
- 为什么constexpr的性能比正常表达式差
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- isspace 函数的性能警告,从 int 转换为布尔值
- 误报警告 PVS 工作室:V821 性能降低。'rhs'变量可以在较低级别的范围内构造
- 如何避免 VS 中unique_ptr签入的性能警告
- 警告 C4800:'int':强制值为 bool 'true' 或 'false'(性能警告)
- 枚举静态转换为布尔,编译器发出性能警告
- 删除C++模板类中的性能警告
- 警告 C4800:'BOOL':强制值为 bool 'true' 或 'false'(性能警告)