Unicode char versus un unicode char, but no warnings or errors

Why does the following code NOT give an error or any type of implicit conversion warning?

std::wstring str = L"hi";
if(str[0] == 'h')
      cout<<"strange"<<endl;

      

Native normal code:

std::wstring str = L"hi";
if(str[0] == L'h')
      cout<<"strange"<<endl;

      

Compiler: visual studio 2005

Warning level: level 4 (highest)

+1


source to share


3 answers


It gives no warning because the comparison is valid. In general, you can always compare integral types, they just rise to wider types as needed.



And I'm sure some compilers will issue a warning about this. Which one are you using? (Warnings are compiler specific anyway, and they don't have to warn about this or anything)

+7


source


Why the following code does NOT give an error ...

Is it because C ++ allows implicit conversions? For example, it is also not legal:

if (str[0] == 104) //C++ allows various implicit type conversions

      



... no type of implicit conversion warning?

This question is compiler specific: Which compiler are you using? There is probably a compiler option that affects the types of warnings you receive from the compiler.

+2


source


It's hard to say why the compiler doesn't give a warning. They can do this for any reason and should do so when the code is questionable. The error would be buggy though, since the code is technically correct.

In this case, my guess is that the compiler does not issue a warning because the compiler uses Unicode wchar_t

and ISO-8859-1 char. The Unicode subset U + 0000 to U + 00FF is equal to ISO 8859-1 characters 0-FF. Thus, each char has the same numeric value as its corresponding wchar_t. As a result wchar_t('a')==L'a'

.

0


source







All Articles