Udostępnij za pośrednictwem


C6328

warning C6328:<type> passed as parameter <number> when <type> is required in call to <function>

For routines starting with is*, passing an argument of type char might yield unpredictable results. For example, an SBCS or MBCS single-byte character of type char with a value greater than 0x7F is negative. If a char is passed, the compiler might convert the value to a signed int or a signed long. This value could be sign-extended by the compiler, with unexpected results. For example, isspace accepts an argument of type int; however, the valid range of values for its input argument is:

0 <= c <= 255, plus the special value EOF.

Example

By default, char is a signed type in Visual C++, so the range of values of a variable of type char is -128 <= c <= 127. Therefore, if you did the following:

#include <iostream>

void f( )

{

char c = -37;

int retVal = isspace( c );

// code...

}

c would be sign-extended to a signed int with a value of -37, which is outside the valid range for isspace.

To correct this problem use static_cast, as shown in the following code:

#include <iostream>

void f( )
{
   char c = -37;
   int retVal = isspace( static_cast<unsigned char> (c) );
   // code...
}

Warning C6328 exists specifically to catch this bug. For characters in the 7-bit ASCII range the cast is unnecessary, but characters outside that range will cause asserts and/or crashes at run time.