Boost C++ Libraries: Ticket #7063: try_unhex() that doesn't throw on bad input https://svn.boost.org/trac10/ticket/7063 <p> Could you provide an unhex() variant that doesn't throw on bad input, but returns an error (non-false) instead? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7063 Trac 1.4.3 Marshall Clow Tue, 17 Jul 2012 20:06:49 GMT <link>https://svn.boost.org/trac10/ticket/7063#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7063#comment:1</guid> <description> <p> unhex already returns a value; the output iterator (so that you can chain operations). </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Wed, 18 Jul 2012 12:47:07 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7063#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7063#comment:2</guid> <description> <p> I know. This request is for something like bool try_unhex(const T&amp; in, U&amp; out) Having to wrap unhex() into a try/catch block is quite cumbersome and sometime exceptions can't be used at all. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Mon, 24 Sep 2012 16:59:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7063#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7063#comment:3</guid> <description> <p> Something like this? </p> <p> Of course, it will still throw if a memory allocation fails. Or the output iterator throws. Or the input iterator throws. Or one of the assignments throw. </p> <pre class="wiki"> template &lt;typename InputIter, typename OutputIter&gt; bool try_unhex ( InputIter first, InputIter last, OutputIter out ) { try { (void) unhex ( first, last, out ); return true; } catch ( const hex_decode_error &amp; ) {} return false; } </pre><p> Note: </p> <ul><li>if you pass the output iterator by value, then you lose any changes that are made. </li><li>If you pass the output iterator by reference, then you can't use an unnamed temporary (such as the result of <code>std::back_inserter</code>) </li></ul><p> In any case, I'm not convinced that this is a good idea. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Thu, 27 Sep 2012 08:27:46 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7063#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7063#comment:4</guid> <description> <p> No, I was thinking of range-based input and container-based output. Like this: </p> <pre class="wiki">std::string a = unhex("ABCD"); // may throw on bad input std::string b; if (!unhex("ABCD", b) return ERROR; </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Thu, 27 Sep 2012 13:16:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7063#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7063#comment:5</guid> <description> <p> Olaf -- </p> <p> Thinking some more about this, I don't think this belongs in boost, but rather in your application code. </p> <p> It throws away useful information, a big no-no in generic code. (The cause of the failure, and the point of failure) </p> <p> Of course, in your specific use case, throwing away this information (or reducing all failures to a single bit) may be perfectly fine, and that's why it should live in your code. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Thu, 27 Sep 2012 13:28:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7063#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7063#comment:6</guid> <description> <p> The point of failure (in the input stream) isn't available anyway, is it? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Thu, 27 Sep 2012 13:52:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7063#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7063#comment:7</guid> <description> <p> Not directly: </p> <ul><li>In the case of "not enough input" the point of failure is the end of the input. </li><li>In the case of "non-hex input", the character that caused the error is attached to the exception that is thrown </li></ul><p> So you can figure out where the error occurred. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Thu, 27 Sep 2012 14:24:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7063#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7063#comment:8</guid> <description> <p> Nice! </p> <pre class="wiki">It throws away useful information, a big no-no in generic code. </pre><p> Since it's a new variant, the old variant is still available if that info is required, isn't it? </p> </description> <category>Ticket</category> </item> </channel> </rss>