c++ - assert(true) 警告签名/未签名不匹配

标签 c++ visual-studio-2008 assert suppress-warnings

Visual Studio 2008,调试版本。这行C++

assert(true);

引起投诉

warning C4365: 'argument' : conversion from 'long' to 'unsigned int', signed/unsigned mismatch

当用任何(有用的) bool 表达式替换 true 时,警告仍然存在,即使是 1ul

仅供引用,编译器的文件 assert.h 是:

#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
extern "C" _CRTIMP void __cdecl _wassert(_In_z_ const wchar_t * _Message, _In_z_ const wchar_t *_File, _In_ unsigned _Line);

如何在不抑制所有 C4365 的情况下完全抑制此警告?是__LINE__的错吗??

最佳答案

The bug report explains it very well :

This issue occurs because __LINE__ is of type long, and the assert macro passes __LINE__ as an argument to the _wassert function, which expects an unsigned int. When not compiling with /ZI, __LINE__ is a constant expression, so the compiler can statically determine that the conversion to unsigned int will result in the same value. When compiling with /ZI, __LINE__ is not a constant expression, so the compiler cannot statically determine that the conversion will result in the same value and it issues warning C4365.

它还提供了一个解决方法:

As a workaround for this issue, I would recommend #undefing assert in your source code, and re-#define-ing it, using the same definition as in <assert.h>, but with a cast to suppress the warning.

请注意,此错误似乎已从 MSVC2015 开始修复。

关于c++ - assert(true) 警告签名/未签名不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35281367/

相关文章:

c++ - vector 与集合的搜索性能

visual-studio - 安装 Team Foundation Server

c# - 用 C# 属性替换 XML 注释的正则表达式

c++ - rxcpp:嵌套的 while 循环或类似的 "classic"程序命令结构

c++ - 使用 vector C++ 的 vector 设置单位矩阵

C++ 简单字符检查

visual-studio-2008 - 在 windows 8.1 中使用 VS2008 创建 Cab 失败

C++ - 完全挂起 Windows 应用程序

java - 用于生产的好的断言类? Java 相当于 Groovy 的 PowerAssert?

python - 如何使用 assertRaises() 捕获 "TypeError"