Boost C++ Libraries: Ticket #7679: unhex level 4 compile warning in visual studio https://svn.boost.org/trac10/ticket/7679 <p> The following code generates a level 4 compiler warning in Visual Studio 2010 and 2012: </p> <pre class="wiki">#include &lt;string&gt; #include &lt;boost/algorithm/hex.hpp&gt; int main() { std::string base = "now is the time"; std::string hstr = boost::algorithm::hex(base); std::string cstr = boost::algorithm::unhex(hstr); } </pre><p> The warnings are: </p> <pre class="wiki">1&gt;d:\development\libs\boost_1_52_0\boost/algorithm/hex.hpp(137): warning C4244: '=' : conversion from 'unsigned int' to 'T', possible loss of data 1&gt; d:\development\libs\boost_1_52_0\boost/algorithm/hex.hpp(204) : see reference to function template instantiation 'std::back_insert_iterator&lt;_Container&gt; boost::algorithm::detail::decode_one&lt;InputIterator,OutputIterator,bool(__cdecl *)(Iterator,Iterator)&gt;(InputIterator &amp;,InputIterator,OutputIterator,EndPred)' being compiled 1&gt; with 1&gt; [ 1&gt; _Container=std::string, 1&gt; InputIterator=std::_String_const_iterator&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt;&gt;, 1&gt; OutputIterator=std::back_insert_iterator&lt;std::string&gt;, 1&gt; Iterator=std::_String_const_iterator&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt;&gt;, 1&gt; EndPred=bool (__cdecl *)(std::_String_const_iterator&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt;&gt;,std::_String_const_iterator&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt;&gt;) 1&gt; ] 1&gt; d:\development\libs\boost_1_52_0\boost/algorithm/hex.hpp(237) : see reference to function template instantiation 'OutputIterator boost::algorithm::unhex&lt;std::_String_const_iterator&lt;_Elem,_Traits,_Alloc&gt;,OutputIterator&gt;(InputIterator,InputIterator,OutputIterator)' being compiled 1&gt; with 1&gt; [ 1&gt; OutputIterator=std::back_insert_iterator&lt;std::string&gt;, 1&gt; _Elem=char, 1&gt; _Traits=std::char_traits&lt;char&gt;, 1&gt; _Alloc=std::allocator&lt;char&gt;, 1&gt; InputIterator=std::_String_const_iterator&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt;&gt; 1&gt; ] 1&gt; d:\development\libs\boost_1_52_0\boost/algorithm/hex.hpp(263) : see reference to function template instantiation 'OutputIterator boost::algorithm::unhex&lt;String,std::back_insert_iterator&lt;_Container&gt;&gt;(const Range &amp;,OutputIterator)' being compiled 1&gt; with 1&gt; [ 1&gt; OutputIterator=std::back_insert_iterator&lt;std::string&gt;, 1&gt; String=std::string, 1&gt; _Container=std::string, 1&gt; Range=std::string 1&gt; ] 1&gt; unhex_bug.cpp(10) : see reference to function template instantiation 'String boost::algorithm::unhex&lt;std::string&gt;(const String &amp;)' being compiled 1&gt; with 1&gt; [ 1&gt; String=std::string 1&gt; ] </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7679 Trac 1.4.3 Marshall Clow Sun, 11 Nov 2012 03:05:15 GMT <link>https://svn.boost.org/trac10/ticket/7679#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7679#comment:1</guid> <description> <p> This is a crap warning. </p> <p> There is no loss of data here. </p> <p> The routine <code>hex_char_to_int</code> only returns values in the range 0..15. </p> <p> With <code>T</code> == <code>char</code>, the loop at line 134 will be executed twice. </p> <p> <code>res</code> is initialized to zero. </p> <p> The first time through the loop, we multiply <code>res</code> (0) * 16 + add the result of <code>hex_char_to_int</code>, so the maximum value that <code>res</code> can be is 15. </p> <p> The second time through the loop, we multiply <code>res</code> * 16 and add the result of <code>hex_char_to_int</code>, so the maximum value that <code>res</code> can be is (15*16) + 15, or 255. </p> <p> No overflow; no loss of data. </p> <p> Please file a bug report with your compiler vendor. </p> </description> <category>Ticket</category> </item> <item> <author>Gary Sanders <lex21@…></author> <pubDate>Tue, 13 Nov 2012 18:50:32 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/7679 https://svn.boost.org/trac10/ticket/7679 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">patchfile.patch</span> </li> </ul> Ticket Gary Sanders <lex21@…> Tue, 13 Nov 2012 18:55:41 GMT <link>https://svn.boost.org/trac10/ticket/7679#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7679#comment:2</guid> <description> <p> I agree that there is no loss of data with regard to the compiler warning. </p> <p> Nevertheless, the attached patch silences the compiler warning referenced above, as well as another compiler warning that indicates that there is unreachable code in hex_char_to_int (the return 0 after the exception is thrown). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Mon, 26 Nov 2012 15:59:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7679#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7679#comment:3</guid> <description> <p> The return that you've added to the end of <code>hex_char_to_int</code> is still unreachable. Other compilers will (potentially) warn on that. </p> <p> You're proposing a substantial pessimization of the code base to eliminate a warning where the compiler is wrong. </p> </description> <category>Ticket</category> </item> <item> <author>Gary Sanders <lex21@…></author> <pubDate>Sun, 02 Dec 2012 17:44:55 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7679#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7679#comment:4</guid> <description> <p> The structure of the proposed patch is as follows: </p> <pre class="wiki">template &lt;typename T&gt; T hex_char_to_int ( T c ) { T r; if (...) r = ...; else if (...) r = ...; else if (...) r = ...; else BOOST_THROW_EXCEPTION (...); return r; } </pre><p> There is one and only one return point. Therefore, the return cannot be un-reachable. </p> <p> The proposed patch, verified on GCC 4.6.3, Visual Studio 2010 and Visual Studio 2012, does not emit warnings. Furthermore, it removes the need for hex_char_to_int reside an anonymous namespace, which could cause link errors if used within multiple translation units. </p> <p> The reality is that even if Microsoft agreed to change their compiler, it will not be done for Visual Studio 2010 or 2012. In the spirit of the last comment in hex_char_to_in, ('keep dump compilers happy') it would be very much appreciated if you would endeavour to keep Visual Studio happy, too. </p> </description> <category>Ticket</category> </item> <item> <author>pmost@…</author> <pubDate>Thu, 07 Apr 2016 13:36:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7679#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7679#comment:5</guid> <description> <p> I'm still experiencing this bug in Boost 1.58.0 with Visual Studio 2013 compiler warning level 4. The problematic code is in hex.hpp(74) </p> <pre class="wiki"> template &lt;typename T&gt; unsigned char hex_char_to_int ( T val ) { char c = static_cast&lt;char&gt; ( val ); unsigned retval = 0; if ( c &gt;= '0' &amp;&amp; c &lt;= '9' ) retval = c - '0'; else if ( c &gt;= 'A' &amp;&amp; c &lt;= 'F' ) retval = c - 'A' + 10; else if ( c &gt;= 'a' &amp;&amp; c &lt;= 'f' ) retval = c - 'a' + 10; else BOOST_THROW_EXCEPTION (non_hex_input() &lt;&lt; bad_char (c)); return retval; } </pre><p> If I define <em>retval</em> to be <em>unsigned char</em> then this warning goes away. Wouldn't this be the simplest solution? </p> </description> <category>Ticket</category> </item> </channel> </rss>