Search code examples
c++visual-studio-2015oracle-pro-c

Getting rid of the warning message - the proper way


I am working on a legacy pro* C/C++ code and migrated the project to Visual Studio 2015. When I compile the code in VS, it is giving me a below warning message at more than 100 places.

warning C4267: '=': conversion from 'size_t' to 'unsigned short', possible loss of data

and the corresponding code is

stmt.len = strlen((char*)stmt.arr); // VARCHAR stmt[500];

I was planning to change the above code to

stmt.len = static_cast<unsigned short>(strlen((char *)stmt.arr));

this will just remove the warning message. But I have to modify in more than 100 places. Is there any way to get rid of this warning message may be using some sort of macro? Please suggest.

Thanks


Solution

  • You can switch off the warning with

    #pragma warning( disable : 4267)
    

    although, personally, I'd work through the errors and fix correctly. Your idea with static_cast isn't a bad one, and there is no danger of undefined behaviour with overflow as you're using unsigned types.

    Finally, note that using a macro to replace a standard library function is undefined behaviour. Don't do that.