assert(true) 警告 signed/unsigned 不匹配

assert(true) warns of signed/unsigned mismatch

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

assert(true);

引起投诉

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

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 开始修复。