c – 断言(true)警告有符号/无符号不匹配
作者:互联网
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开始修复.
标签:c,assert,suppress-warnings,visual-studio-2008 来源: https://codeday.me/bug/20190829/1760316.html