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/