Boost C++ Libraries: Ticket #6700: Exceptionless lexical_cast https://svn.boost.org/trac10/ticket/6700 <p> Could you provide an exceptionless lexical_cast variant? </p> <pre class="wiki">bool try_lexical_cast&lt;Out, In&gt;(Out&amp;, const In&amp;) </pre><p> Exceptions aren't always the best option. Sometimes they're not even an option. Could you provide an exceptionless variant? As bonus it's no longer necessary to specify the Out template argument. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6700 Trac 1.4.3 Antony Polukhin Sat, 06 Oct 2012 20:17:26 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/6700#comment:1 https://svn.boost.org/trac10/ticket/6700#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">wontfix</span> </li> </ul> <p> Currently it is not possible, because I can not provide strong exception guarantee. For example, lexical_cast uses std::locale, which can throw. String constructors, some std::stream operators, user defined ostream and istream operators may also throw. </p> Ticket Olaf van der Spek <olafvdspek@…> Sat, 06 Oct 2012 22:29:56 GMT <link>https://svn.boost.org/trac10/ticket/6700#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:2</guid> <description> <p> I'm not asking for a variant that doesn't throw at all, just asking for a variant that doesn't throw itself. If some other code throws, that's ok. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sun, 07 Oct 2012 19:49:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/6700#comment:2" title="Comment 2">Olaf van der Spek &lt;olafvdspek@…&gt;</a>: </p> <blockquote class="citation"> <p> I'm not asking for a variant that doesn't throw at all, just asking for a variant that doesn't throw itself. If some other code throws, that's ok. </p> </blockquote> <p> <br /> I like the idea of try_lexical_cast, but only if it provides strong exception guarantee. Otherwise it may obfuscate users and is not as useful as function with strong exception guarantee. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Sun, 07 Oct 2012 22:42:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:4</guid> <description> <p> I assume that's doable. </p> </description> <category>Ticket</category> </item> <item> <author>ben.maurer@…</author> <pubDate>Wed, 11 Dec 2013 00:06:08 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:5</guid> <description> <p> Any update on if you'd consider doing this? At Facebook we've seen many instances of programs that call lexical_cast in a try/catch. In cases where the parsing is expected to fail (eg, an argument could be an int or a date) we frequently see extremely poor performance in multithreading. GCC is known to have poor multithreaded exception handling gcc.gnu.org/ml/gcc/2013-05/msg00253.html </p> <p> Facebook is currently running a bug bounty program for open source bugs, and this is a bug that might be worthy of such a bounty, if is something there is interest in merging. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Wed, 11 Dec 2013 05:37:23 GMT</pubDate> <title>status changed; resolution deleted https://svn.boost.org/trac10/ticket/6700#comment:6 https://svn.boost.org/trac10/ticket/6700#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">wontfix</span> </li> </ul> <p> This issue hit me in my own projects. </p> <p> I'll create a separate branch at github with <code>try_lexical_cast</code> and ask for mini review at mailing lists when it'll be ready. </p> Ticket ben.maurer@… Wed, 11 Dec 2013 19:42:21 GMT <link>https://svn.boost.org/trac10/ticket/6700#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:7</guid> <description> <p> Thanks! Facebook is putting a $200 bounty on this via bountysource: www.bountysource.com/issues/1352296 </p> </description> <category>Ticket</category> </item> <item> <author>Troy Korjuslommi <troykor@…></author> <pubDate>Thu, 30 Jan 2014 07:17:03 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/6700 https://svn.boost.org/trac10/ticket/6700 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_1_55_0_lexical_cast_hpp.patch</span> </li> </ul> <p> Patch to add try_lexical_cast function. Patch is against boost_1_55_0 lexical_cast.hpp. </p> Ticket Troy Korjuslommi <troykor@…> Thu, 30 Jan 2014 07:21:37 GMT <link>https://svn.boost.org/trac10/ticket/6700#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:8</guid> <description> <p> I added the patch here. </p> <p> try_lexical_cast: A version which doesn't throw bad_lexical_cast. All other exceptions will be thrown as normal. </p> <p> Getting rid of exceptions thrown in the callbacks nothrow_overflow_handler and detect_precision_loss would mean rewriting boost::numeric::converter, which seems excessive, and futile. And it would create a dependency on those patches going through for this patch to be accepted. Ergo, I resorted to catching these exceptions to provide non throwing behavior. </p> <p> All other instances where an instance of bad_lexical_cast could be thrown have been replaced with a new function. </p> <p> Dropped the deprecated call-by-value fallback version template&lt;typename Target, typename Source&gt; bool bool try_lexical_cast(Target&amp; result, Source arg); </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Thu, 30 Jan 2014 08:32:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:9</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/6700#comment:8" title="Comment 8">Troy Korjuslommi &lt;troykor@…&gt;</a>: </p> <blockquote class="citation"> <p> I added the patch here. </p> </blockquote> <p> That's a bad patch. It breaks compilation with exceptions off. It does not add tests for new feature and does not update the docs. </p> <p> It is also much worse than an already applied <a class="ext-link" href="https://github.com/boostorg/conversion/tree/develop"><span class="icon">​</span>to develop branch patch</a>: </p> <p> <code></code><code>try {} catch(...){}</code><code></code> is not what was requested by users. <code>boost::numeric::converter</code> was the main problem! Without fixing it a patch could be written in 10 minutes, but that would be a patch for outward appearances, does not really resolving the issue. </p> </description> <category>Ticket</category> </item> <item> <author>Troy Korjuslommi <troykor@…></author> <pubDate>Thu, 30 Jan 2014 09:16:33 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:10</guid> <description> <p> I can post proper tests and docs once this patch gets some positive comments. The comments in this ticket seem sufficient for reviewing the patch itself. </p> <p> The lexical_cast function doesn't compile with -DBOOST_NO_EXCEPTIONS either, and this ticket had no mention of it. I did consider it, but it didn't seem to be an concern here. I guess I was wrong. I will see what needs to be done. </p> <p> The patch against numeric::converter should be developed independently, as it's a separate package. Once it works, try_lexical_cast can be modified to call it's non throwing function. That's a one line change. </p> <p> As I see it, the patch solves the issue at hand. Adding the -DBOOST_NO_EXCEPTIONS support and calls to modified numeric::converter can be added once the basic functionality is approved. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Thu, 30 Jan 2014 09:40:38 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:11</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/6700#comment:10" title="Comment 10">Troy Korjuslommi &lt;troykor@…&gt;</a>: </p> <blockquote class="citation"> <p> I can post proper tests and docs once this patch gets some positive comments. The comments in this ticket seem sufficient for reviewing the patch itself. </p> </blockquote> <p> Once again: the issue is already resolved in developer branch of Boost. </p> <blockquote class="citation"> <p> The lexical_cast function doesn't compile with -DBOOST_NO_EXCEPTIONS either, and this ticket had no mention of it. I did consider it, but it didn't seem to be an concern here. I guess I was wrong. I will see what needs to be done. </p> </blockquote> <p> You are wrong. <code>lexical_cast function</code> <strong>does</strong> compile with -DBOOST_NO_EXCEPTIONS. See <a href="http://www.boost.org/development/tests/develop/developer/conversion.html">results</a> for <code>lexical_cast_no_exceptions_test</code>. And it compiles for a long time! </p> <blockquote class="citation"> <p> The patch against numeric::converter should be developed independently, as it's a separate package. Once it works, try_lexical_cast can be modified to call it's non throwing function. That's a one line change. </p> <p> As I see it, the patch solves the issue at hand. Adding the -DBOOST_NO_EXCEPTIONS support and calls to modified numeric::converter can be added once the basic functionality is approved. </p> </blockquote> <p> Once again: the <strong>issue is already resolved</strong> in developer branch of Boost. It is waiting for regression tests to pass, before merging to release. And it is resolved without breaking existing functionality and without "let's do it wrong for now". </p> <p> If you want to help, try fixing this issue <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8261" title="#8261: Bugs: lexical_cast&lt;unsigned&gt; returns unexpected result when using with ... (closed: duplicate)">#8261</a>. </p> <p> If you want to get some easy money from <a class="ext-link" href="https://www.bountysource.com/issues/1352296-exceptionless-lexical_cast"><span class="icon">​</span>bountysource</a>, then <strong>do the work right</strong>! Do the work that is <strong>not finished</strong>! </p> </description> <category>Ticket</category> </item> <item> <author>Troy Korjuslommi <troykor@…></author> <pubDate>Thu, 30 Jan 2014 10:16:03 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:12</guid> <description> <p> Next time you work on something, please mention it here too, and provide a link for others to see your work. Now I wasted my time. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Thu, 30 Jan 2014 10:52:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:13</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/6700#comment:12" title="Comment 12">Troy Korjuslommi &lt;troykor@…&gt;</a>: </p> <blockquote class="citation"> <p> Next time you work on something, please mention it here too, and provide a link for others to see your work. Now I wasted my time. </p> </blockquote> <p> Mention at <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/6700?replyto=12#comment:6"><span class="icon">​</span>comment 6</a> does not work for you? </p> <p> It's a shame that "getting new knowledge" for you is "wasting time". </p> </description> <category>Ticket</category> </item> <item> <author>Troy Korjuslommi <troykor@…></author> <pubDate>Sat, 01 Feb 2014 10:06:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6700#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6700#comment:14</guid> <description> <p> I will have some time later today, so I will have a look at the <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8261" title="#8261: Bugs: lexical_cast&lt;unsigned&gt; returns unexpected result when using with ... (closed: duplicate)">#8261</a> issue. </p> <p> I did look for try_lexical_cast under github, but it wasn't there. I assumed you had abandoned the task. I realize now I should have used google and not github's own search function to look for the project, since you had apparently changed your mind and posted it under conversion instead of creating a try_lexical_cast entry. </p> <p> Because I didn't see the entry, I used the latest version of boost, 1.55.0 as the basis for the changes. This seemed sensible at the time. 1.55.0's lexical_cast does not compile with -DBOOST_NO_EXCEPTIONS, but the development branch does. There's the cause for this miscommunication. </p> <p> Anyway, no need to be upset. I wasn't trying to step on your toes, just thought I'd contribute something. You have the changes under control. That's great. I will have a look at the problem with <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8261" title="#8261: Bugs: lexical_cast&lt;unsigned&gt; returns unexpected result when using with ... (closed: duplicate)">#8261</a> and let's see if I can help there. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Tue, 04 Mar 2014 12:52:27 GMT</pubDate> <title>status, milestone changed; resolution set https://svn.boost.org/trac10/ticket/6700#comment:15 https://svn.boost.org/trac10/ticket/6700#comment:15 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.56.0</span> </li> </ul> <p> <a class="ext-link" href="https://github.com/boostorg/conversion/commit/9e6aa219d30d182123edc0866362933cce99e6cf"><span class="icon">​</span>Merged</a> to master branch, after ensuring that test failures are not related to try_lexical_convert changes. </p> Ticket