Boost C++ Libraries: Ticket #2295: Inconsistent behavior when using 64 bit integer types https://svn.boost.org/trac10/ticket/2295 <p> 1) Converting 64bit unsigned int to it's max value string representation and back results in exception (should be identity operation!): </p> <pre class="wiki">boost::uint64_t max = numeric_limits&lt;boost::uint64_t&gt;::max(); std::string s = lexical_cast&lt;string&gt;(max); BOOST_CHECK_EQUAL(max, lexical_cast&lt;boost::uint64_t&gt;(s)); // crashes: std::bad_cast: bad lexical cast: source type value could not be interpreted as target </pre><p> 2) Precise conditions when the conversion will fail are not specified in the documentation. Throwing of bad_lexical_cast is inconsistent: </p> <pre class="wiki">#define T short // add a digit to a min representation! s = lexical_cast&lt;string, T&gt;(numeric_limits&lt;T&gt;::min()) + "0"; BOOST_CHECK_THROW(lexical_cast&lt;T&gt;(s), boost::exception); // throws s = lexical_cast&lt;string, T&gt;(numeric_limits&lt;T&gt;::min()) + "5"; BOOST_CHECK_THROW(lexical_cast&lt;T&gt;(s), boost::exception); // throws #define T boost::int64_t s = lexical_cast&lt;string, T&gt;(numeric_limits&lt;T&gt;::min()) + "0"; BOOST_CHECK_THROW(lexical_cast&lt;T&gt;(s), boost::exception); // DOES NOT throw!?!? s = lexical_cast&lt;string, T&gt;(numeric_limits&lt;T&gt;::min()) + "5"; BOOST_CHECK_THROW(lexical_cast&lt;T&gt;(s), boost::exception); // throws </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2295 Trac 1.4.3 Hrvoje Prgeša <hrvoje.prgesa@…> Sat, 06 Sep 2008 18:43:30 GMT <link>https://svn.boost.org/trac10/ticket/2295#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2295#comment:1</guid> <description> <p> Note: tested on MS VC9 (without SP1) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 28 Nov 2008 13:28:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2295#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2295#comment:2</guid> <description> <p> Update: the 2. problem seems to be caused by the MSVC streams bug, and the 1. might also probably caused by it. </p> <p> Nevertheless, lexical_cast tests really should include tests for 64bit integer types. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>nasonov</dc:creator> <pubDate>Thu, 04 Dec 2008 11:04:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2295#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2295#comment:3</guid> <description> <blockquote class="citation"> <p> Nevertheless, lexical_cast tests really should include tests for 64bit integer types. </p> </blockquote> <p> They do. Search for LCAST_TEST_LONGLONG in <a class="ext-link" href="http://svn.boost.org/svn/boost/branches/release/libs/conversion/lexical_cast_test.cpp"><span class="icon">​</span>http://svn.boost.org/svn/boost/branches/release/libs/conversion/lexical_cast_test.cpp</a> </p> <p> Though, I don't understand/remember why it uses <span class="underline">int64 rather than boost::long_long_type. </span></p> <p> I should also add a test for boost::intmax_t type. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 04 Dec 2008 19:11:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2295#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2295#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/2295#comment:3" title="Comment 3">nasonov</a>: </p> <blockquote class="citation"> <blockquote class="citation"> <p> Nevertheless, lexical_cast tests really should include tests for 64bit integer types. </p> </blockquote> <p> They do. Search for LCAST_TEST_LONGLONG in <a class="ext-link" href="http://svn.boost.org/svn/boost/branches/release/libs/conversion/lexical_cast_test.cpp"><span class="icon">​</span>http://svn.boost.org/svn/boost/branches/release/libs/conversion/lexical_cast_test.cpp</a> </p> </blockquote> <p> They do indeed. Didn't notice it before when I was looking. </p> <blockquote class="citation"> <p> Though, I don't understand/remember why it uses <span class="underline">int64 rather than boost::long_long_type. </span></p> </blockquote> <p> The link you posted tests boost::long_long_type if BOOST_HAS_LONG_LONG is defined (and seems fall back on <span class="underline">int64 defending on few other defined). </span></p> <blockquote class="citation"> <p> I should also add a test for boost::intmax_t type. </p> </blockquote> <p> May I suggest adding more tests that check conversion failure in the generic testing routines - there is currently not a single one failure test in them (and therefore not a single failure test for long long), there are only tests for around min, max, 0 and exp(10). </p> <p> Eg. for "test_conversion_from_integral_to_string" something like my second use case would fit nicely: typedef std::basic_string&lt;CharT&gt; string_type; for(i = 0; i &lt;= 10; ++i) { </p> <blockquote> <p> string_type underflow_str = to_str&lt;CharT&gt;(limits::min()) + to_str&lt;CharT&gt;(i); BOOST_CHECK_THROW(lexical_cast&lt;T&gt;(underflow_str), bad_lexical_cast); </p> </blockquote> <blockquote> <p> string_type overflow_str = to_str&lt;CharT&gt;(limits::max()) + to_str&lt;CharT&gt;(i); BOOST_CHECK_THROW(lexical_cast&lt;T&gt;(overflow_str), bad_lexical_cast); </p> </blockquote> <p> } (this check overflow and underflow by appending digits, not by strictly increasing the min/max by +/-1, although that could also be done.) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>nasonov</dc:creator> <pubDate>Sun, 13 Sep 2009 14:48:29 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2295#comment:5 https://svn.boost.org/trac10/ticket/2295#comment:5 <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">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/56170" title="Fix #2295 (Inconsistent behavior when using 64 bit integer types). ">[56170]</a>) Fix <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2295" title="#2295: Bugs: Inconsistent behavior when using 64 bit integer types (closed: fixed)">#2295</a> (Inconsistent behavior when using 64 bit integer types). </p> Ticket