为什么warning C4804: '>': unsafe use of type 'bool' in operation
会出现在 Visual Studio 2015 上?
如果您运行此代码:
#include <iostream>
#include <cstdlib>
int main( int argumentsCount, char* argumentsStringList[] )
{
#define COMPUTE_DEBUGGING_LEVEL_DEBUG 0
#define COMPUTE_DEBUGGING_DEBUG_INPUT_SIZE 32
int inputLevelSize;
int builtInLevelSize;
inputLevelSize = strlen( "a1" );
builtInLevelSize = strlen( "a1 a2" );
if( ( 2 > inputLevelSize > COMPUTE_DEBUGGING_DEBUG_INPUT_SIZE )
|| ( 2 > builtInLevelSize > COMPUTE_DEBUGGING_DEBUG_INPUT_SIZE ) )
{
std::cout << "ERROR while processing the DEBUG LEVEL: " << "a1" << std::endl;
exit( EXIT_FAILURE );
}
}
你会得到:
./cl_env.bat /I. /EHsc /Femain.exe main.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
main.cpp
main.cpp(52): warning C4804: '>': unsafe use of type 'bool' in operation
main.cpp(53): warning C4804: '>': unsafe use of type 'bool' in operation
Microsoft (R) Incremental Linker Version 14.00.23506.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:main.exe
main.obj
在哪里cl_env.bat
:
@echo off
:: Path to your Visual Studio folder.
::
:: Examples:
:: C:\Program Files\Microsoft Visual Studio 9.0
:: F:\VisualStudio2015
set VISUAL_STUDIO_FOLDER=F:\VisualStudio2015
:: Load compilation environment
call "%VISUAL_STUDIO_FOLDER%\VC\vcvarsall.bat"
:: Invoke compiler with any options passed to this batch file
"%VISUAL_STUDIO_FOLDER%\VC\bin\cl.exe" %*
有问题的行不是布尔值:
if( ( 2 > inputLevelSize > COMPUTE_DEBUGGING_DEBUG_INPUT_SIZE )
|| ( 2 > builtInLevelSize > COMPUTE_DEBUGGING_DEBUG_INPUT_SIZE ) )
如何正确地做表达式 as 0 < x < 10
?
所说的内容与解释器有关。例子:
当 C++ 标准规定编译器必须理解
0 < x < 10
为( 0 < x ) && ( x < 10 )
,但编译器实际上将其理解为( 0 < x ) < 10
时,我们称其为编译器错误。因此,当用户声明编译器必须理解
0 < x < 10
为 as( 0 < x ) && ( x < 10 )
时,编译器实际上将其理解为 as( 0 < x ) < 10
时,我们称其为用户错误。