dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Weird warning after MSVC 2010 -> 2017

  1. #1
    Join Date
    Aug 2002
    Posts
    681

    Weird warning after MSVC 2010 -> 2017

    Hi,
    I am starting to get following on this line of code:

    Code:
            std::wstring s;
            std::transform( s.begin(), s.end(), s.begin(), toupper );
    Code:
    3>  c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\algorithm(1404): warning C4244: '=': conversion from 'int' to 'wchar_t', possible loss of data (compiling source file database_sqlite.cpp)
    3>  xxx.cpp note: see reference to function template instantiation '_OutIt std::transform<std::_String_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::_String_iterator<std::_String_val<std::_Simple_types<_Ty>>>,int(__cdecl *)(int)>(const _InIt,const _InIt,_OutIt,_Fn)' being compiled
    3>          with
    3>          [
    3>              _OutIt=std::_String_iterator<std::_String_val<std::_Simple_types<wchar_t>>>,
    3>              _Ty=wchar_t,
    3>              _InIt=std::_String_iterator<std::_String_val<std::_Simple_types<wchar_t>>>,
    3>              _Fn=int (__cdecl *)(int)
    3>          ]
    Can someone explain to me why this warning start to show up?

    Thank you.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,700

    Re: Weird warning after MSVC 2010 -> 2017

    What is toupper defined as?

  3. #3
    Join Date
    Feb 2017
    Posts
    320

    Re: Weird warning after MSVC 2010 -> 2017

    Quote Originally Posted by OneEyeMan View Post
    Can someone explain to me why this warning start to show up?
    It's probably that you compile with a higher warning level now than before.

    If you use std::towupper instead the warning should disappear.
    Last edited by wolle; August 1st, 2018 at 11:01 PM.

  4. #4
    Join Date
    Aug 2002
    Posts
    681

    Re: Weird warning after MSVC 2010 -> 2017

    Hi,
    Quote Originally Posted by wolle View Post
    It's probably that you compile with a higher warning level now than before.
    No, warning level is the same.

    Quote Originally Posted by wolle View Post
    If you use std::towupper instead the warning should disappear.
    [/quote]

    Changed the code as such and still got same warning.

    Thank you.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,195

    Re: Weird warning after MSVC 2010 -> 2017

    Quote Originally Posted by OneEyeMan View Post
    Hi,

    No, warning level is the same.
    ...
    Changed the code as such and still got same warning.

    Thank you.
    Have a look at https://developercommunity.visualstu...g-level-4.html
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,354

    Re: Weird warning after MSVC 2010 -> 2017

    The underlying type of wstring is wchar_t. wchar_t is a short int (16 bits).

    int toupper(int a)

    This takes an int argument (not a char) and returns an int (usually 32 bits) - not a char! This is greater size than 16 bits for wchar_t hence the warning re possible loss of data for conversion from wchar_t to int.

    int towupper(wint_t a)

    This takes a wint_t argument (which is a short int 16 bits) and returns an int. Again, there is a data narrowing from int to wchar_t (32 to 16 bits) hence the warning.

    Note that if wstring etc is used, then the corresponding towupper(), iswlower() etc should be used and not the ASCII versions (toupper(), islower) etc).

    To get around this and avoid the warning, the result from towupper() needs to be cast to the correct type. So transform() needs a lambda as the last param. Consider

    Code:
    transform(s.begin(), s.end(), s.begin(),  [](auto ch){return static_cast<wchar_t>(std::towupper(ch));});
    Also note that for c++, these functions are in the std:: namespace and not the global namespace and that the include file is cctype and not ctype.h .
    Last edited by 2kaud; August 2nd, 2018 at 07:51 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.6)

  7. #7
    Join Date
    Aug 2002
    Posts
    681

    Re: Weird warning after MSVC 2010 -> 2017

    Hi,
    Quote Originally Posted by 2kaud View Post
    The underlying type of wstring is wchar_t. wchar_t is a short int (16 bits).

    int toupper(int a)

    This takes an int argument (not a char) and returns an int (usually 32 bits) - not a char! This is greater size than 16 bits for wchar_t hence the warning re possible loss of data for conversion from wchar_t to int.

    int towupper(wint_t a)

    This takes a wint_t argument (which is a short int 16 bits) and returns an int. Again, there is a data narrowing from int to wchar_t (32 to 16 bits) hence the warning.

    Note that if wstring etc is used, then the corresponding towupper(), iswlower() etc should be used and not the ASCII versions (toupper(), islower) etc).

    To get around this and avoid the warning, the result from towupper() needs to be cast to the correct type. So transform() needs a lambda as the last param. Consider

    Code:
    transform(s.begin(), s.end(), s.begin(),  [](auto ch){return static_cast<wchar_t>(std::towupper(ch));});
    Also note that for c++, these functions are in the std:: namespace and not the global namespace and that the include file is cctype and not ctype.h .
    I changed rhe code to read:

    Code:
            std::transform( s.begin(), s.end(), s.begin(), [](auto ch) {return static_cast<wchar_t>( std::towupper( c ) ); } );
    and included cctype.

    Now I'm getting:

    Code:
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\algorithm(1404): warning C4244: '=': conversion from 'int' to 'wchar_t', possible loss of data
    1>foo.cpp(1267): error C2039: 'towupper': is not a member of 'std'
    1>foo.cpp(1267): error C2065: 'c': undeclared identifier
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\algorithm(1404): error C2440: '=': cannot convert from 'void' to 'wchar_t'
    Thank you.
    Last edited by 2kaud; August 2nd, 2018 at 07:51 AM.

  8. #8
    Join Date
    Aug 2002
    Posts
    681

    Re: Weird warning after MSVC 2010 -> 2017

    2kaud,
    After fixing the include file name to be "cwctype" (for std::wstring) and fixing the ch<->c compilation error I still get this warning.

    Thank you.

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,354

    Re: Weird warning after MSVC 2010 -> 2017

    Sorry, the include file for the w versions is cwctype and not cctype.

    This compiles ok

    Code:
    #include <string>
    #include <cwctype>
    #include <algorithm>
    
    int main()
    {
    	std::wstring s = L"qwerty";
    
    	std::transform(s.begin(), s.end(), s.begin(), [](auto ch) {return static_cast<wchar_t>(std::towupper(ch));});
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.6)

  10. #10
    Join Date
    Aug 2002
    Posts
    681

    Re: Weird warning after MSVC 2010 -> 2017

    2kaud,
    Here is my latest code:

    Code:
    std::transform( s.begin(), s.end(), s.begin(), [](auto ch) { return static_cast<wchar_t>( std::towupper( ch ) ); } );
    and I still see following:

    Code:
    3>  c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\algorithm(1404): warning C4244: '=': conversion from 'int' to 'wchar_t', possible loss of data (compiling source file database_sqlite.cpp)
    3>  foo.cpp(1283): note: see reference to function template instantiation '_OutIt std::transform<std::_String_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::_String_iterator<std::_String_val<std::_Simple_types<_Ty>>>,int(__cdecl *)(int)>(const _InIt,const _InIt,_OutIt,_Fn)' being compiled
    3>          with
    3>          [
    3>              _OutIt=std::_String_iterator<std::_String_val<std::_Simple_types<wchar_t>>>,
    3>              _Ty=wchar_t,
    3>              _InIt=std::_String_iterator<std::_String_val<std::_Simple_types<wchar_t>>>,
    3>              _Fn=int (__cdecl *)(int)
    3>          ]
    I am compiling with the "Level 4" warnings.

    Thank you.

    P.S.: Why there is no "case-incensitive compare" function in C++ {11}? ;-)
    Last edited by OneEyeMan; August 2nd, 2018 at 10:28 AM.

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,354

    Re: Weird warning after MSVC 2010 -> 2017

    The code in post #9 was compiled with level 4 warnings and compiled clean. Are you looking at the right source line? The first line references database_sqlite.cpp and the second foo.cpp .

    Why there is no "case-incensitive compare" function in C++
    There is for windows. See https://docs.microsoft.com/en-us/cpp...mp-l-mbsicmp-l
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.6)

  12. #12
    Join Date
    Aug 2002
    Posts
    681

    Re: Weird warning after MSVC 2010 -> 2017

    2kaud,
    I apologize I did check the wrong file/line number.
    Now, it did compile successfully on MSVC 2017, but with gcc on c++11 it gave an error:

    Code:
    /home/igor/dbhandler/libsqlite/database_sqlite.cpp:1267:59: error: use of ‘auto’ in lambda parameter declaration only available with -std=c++14 or -std=gnu++14
    Will it work if I change "auto" to "wchar_t"? Or this will not work for gcc?
    I just want to have as little conditional compilations as possible.

    Thank you.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,195

    Re: Weird warning after MSVC 2010 -> 2017

    Quote Originally Posted by OneEyeMan View Post
    2kaud,
    I apologize I did check the wrong file/line number.
    Now, it did compile successfully on MSVC 2017, but with gcc on c++11 it gave an error:

    Code:
    /home/igor/dbhandler/libsqlite/database_sqlite.cpp:1267:59: error: use of ‘auto’ in lambda parameter declaration only available with -std=c++14 or -std=gnu++14
    Will it work if I change "auto" to "wchar_t"? Or this will not work for gcc?
    ...
    Why don't you want to test it?
    I guess it would be faster than post the question to the forum and then wait for reply...
    Victor Nijegorodov

  14. #14
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,354

    Re: Weird warning after MSVC 2010 -> 2017

    Quote Originally Posted by OneEyeMan View Post
    2kaud,
    I apologize I did check the wrong file/line number.
    Now, it did compile successfully on MSVC 2017, but with gcc on c++11 it gave an error:

    Code:
    /home/igor/dbhandler/libsqlite/database_sqlite.cpp:1267:59: error: use of ‘auto’ in lambda parameter declaration only available with -std=c++14 or -std=gnu++14
    Will it work if I change "auto" to "wchar_t"? Or this will not work for gcc?
    I just want to have as little conditional compilations as possible.

    Thank you.
    Well the error message says how to fix it - use the specified options (assuming you're using the latest version of gcc). If you have got the latest gcc version, then use -std=c++1z option to enable c++17 features.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.6)

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,700

    Re: Weird warning after MSVC 2010 -> 2017

    Quote Originally Posted by VictorN View Post
    Why don't you want to test it?
    I guess it would be faster than post the question to the forum and then wait for reply...
    Wondering the same thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)