danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
715 stars 163 forks source link

Fail to compile jsoncons 0.170.0 with Visual Studio 2019 (Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30148 for x64) #420

Closed AndreyAlifanov closed 1 year ago

AndreyAlifanov commented 1 year ago

Hi,

I try to compile new version of jsoncons and get next errors (I know: these are warinngs but in our project all warnings treated as errors).

11>D:\AP\ThirdProjects\JSONCons\jsoncons\detail\parse_number.hpp(331,35): error C2220: the following warning is treated as an error (compiling source file Pi\PiJsonImpl.cpp)
11>D:\AP\ThirdProjects\JSONCons\jsoncons_ext\jsonpointer\jsonpointer.hpp(250): message : see reference to function template instantiation 'jsoncons::detail::to_integer_result<uint8_t,_Elem> jsoncons::detail::to_integer_base16<uint8_t,_Elem>(const CharT *,size_t,T &)' being compiled
11>        with
11>        [
11>            _Elem=wchar_t,
11>            CharT=wchar_t,
11>            T=uint8_t
11>        ] (compiling source file Pi\PiJsonImpl.cpp)
11>D:\AP\ThirdProjects\JSONCons\jsoncons_ext\jsonpointer\jsonpointer.hpp(237): message : while compiling class template member function 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> jsoncons::jsonpointer::basic_json_pointer<wchar_t>::unescape_uri_string(const std::basic_string_view<wchar_t,std::char_traits<wchar_t>> &,std::error_code &)' (compiling source file Pi\PiJsonImpl.cpp)
11>D:\AP\ThirdProjects\JSONCons\jsoncons_ext\jsonpointer\jsonpointer.hpp(128): message : see reference to function template instantiation 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> jsoncons::jsonpointer::basic_json_pointer<wchar_t>::unescape_uri_string(const std::basic_string_view<wchar_t,std::char_traits<wchar_t>> &,std::error_code &)' being compiled (compiling source file Pi\PiJsonImpl.cpp)
11>D:\AP\Mod\PiLib\Pi\PiJsonImpl.cpp(613): message : see reference to class template instantiation 'jsoncons::jsonpointer::basic_json_pointer<wchar_t>' being compiled
11>D:\AP\ThirdProjects\JSONCons\jsoncons\detail\parse_number.hpp(331,35): warning C4244: '=': conversion from 'int' to 'T', possible loss of data
11>        with
11>        [
11>            T=uint8_t
11>        ] (compiling source file Pi\PiJsonImpl.cpp)
11>D:\AP\ThirdProjects\JSONCons\jsoncons\detail\parse_number.hpp(334,35): warning C4244: '=': conversion from 'int' to 'T', possible loss of data
11>        with
11>        [
11>            T=uint8_t
11>        ] (compiling source file Pi\PiJsonImpl.cpp)
11>D:\AP\ThirdProjects\JSONCons\jsoncons\detail\parse_number.hpp(337,35): warning C4244: '=': conversion from 'int' to 'T', possible loss of data
11>        with
11>        [
11>            T=uint8_t
11>        ] (compiling source file Pi\PiJsonImpl.cpp)

Version 0.167.1 did not have such warnings and errors.

I think it should be something like this:

331    x = static_cast<T>(c - '0');
334    x = static_cast<T>(c - ('a' - 10));
337    x = static_cast<T>(c - ('A' - 10));
danielaparker commented 1 year ago

Thanks for reporting this warning. It looks like you're using the wchar_t interface, the real issue is that we don't have sufficient test coverage for the wchar_t interface, so this warning wasn't being detected in our tests. I've added tests that cover the cases where c - '0' is used in wstring to binary conversions, and reproduced your issue, and a couple of others. Fixed on master.

AndreyAlifanov commented 1 year ago

Thanks for quick fix!!!