Boost C++ Libraries: Ticket #8124: No more exception when casting from negative number string to unsigned https://svn.boost.org/trac10/ticket/8124 <p> With gcc 4.2.4, a lexical_cast from negative number strings to an unsigned causes a bad_lexical_cast exception. With gcc 4.7.1 and 4.7.2, the value interpreted as positive integer is assigned instead. This bug occurs with Boost 1.33 and 1.51 on Open Suse and Solaris 10. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8124 Trac 1.4.3 Bernd Raum <braum@…> Wed, 20 Feb 2013 08:17:24 GMT attachment set https://svn.boost.org/trac10/ticket/8124 https://svn.boost.org/trac10/ticket/8124 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_lc.cpp</span> </li> </ul> Ticket Antony Polukhin Sun, 24 Feb 2013 07:28:33 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8124#comment:1 https://svn.boost.org/trac10/ticket/8124#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> I'm afraid that this is not an error. </p> <p> boost::lexical_cast has the behavior of std::stringstream, which uses num_get functions of std::locale to convert numbers. If we look at the Programming languages — C++, we'll see, that num_get uses the rules of scanf for conversions. And in the C99 standard for unsigned input value minus sign is optional, so if a negative number is read, no errors will arise and the result will be the two's complement. </p> <p> I do not like such behaviour, but nothing can be done. </p> <p> Some code to demonstrate that lexical_cast works exactly like std::stringstream </p> <pre class="wiki">#include &lt;boost/lexical_cast.hpp&gt; #include &lt;sstream&gt; #include &lt;assert.h&gt; int main() { std::stringstream ss("-1"); unsigned int i; ss &gt;&gt; i; assert(boost::lexical_cast&lt;unsigned int&gt;("-1") == i); } </pre> Ticket