Boost C++ Libraries: Ticket #5357: system::error_code::operator unspecified_bool_type() should not assume 0 == success. https://svn.boost.org/trac10/ticket/5357 <p> [The following was submitted for discussion to boost@… on 2011-03-10. It received only one response, which was supportive.] </p> <p> I've found boost.system to be very useful for wrapping return codes from 3rd party libraries (e.g. libcurl). However, one problem I've encountered is that error_code's operator for boolean tests assumes that an error value of 0 == success. While this is often true, it is not universally so and precludes use of boost.system for such tasks as wrapping HTTP status codes (several of which are merely informative and don't indicate an error). </p> <p> I've found it puzzling that the logic for determining whether an error value represents an error doesn't reside in the error_category, where it can be customized as needed. For instance, boolean tests could chain to a is_error() virtual method in the error_category, the default implementation of which preserves the current behavior: </p> <div class="wiki-code"><div class="code"><pre> <span class="k">namespace</span> <span class="n">boost</span> <span class="p">{</span> <span class="k">namespace</span> <span class="n">system</span> <span class="p">{</span> <span class="k">class</span> <span class="nc">error_category</span> <span class="o">:</span> <span class="k">public</span> <span class="n">noncopyable</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="c1">// ...</span> <span class="k">virtual</span> <span class="kt">bool</span> <span class="n">is_error</span><span class="p">(</span> <span class="kt">int</span> <span class="n">ev</span> <span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">ev</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="c1">// ...</span> <span class="p">};</span> <span class="k">class</span> <span class="nc">error_code</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="c1">// ...</span> <span class="k">operator</span> <span class="n">unspecified_bool_type</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">m_cat</span><span class="o">-&gt;</span><span class="n">is_error</span><span class="p">(</span> <span class="n">m_val</span> <span class="p">)</span> <span class="o">?</span> <span class="nl">unspecified_bool_true</span> <span class="p">:</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="c1">// ...</span> <span class="p">};</span> <span class="p">}</span> <span class="p">}</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5357 Trac 1.4.3 viboes Sat, 25 Aug 2012 08:35:05 GMT cc set https://svn.boost.org/trac10/ticket/5357#comment:1 https://svn.boost.org/trac10/ticket/5357#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">viboes</span> added </li> </ul> <p> The code I'm seen in is </p> <pre class="wiki"> operator unspecified_bool_type() const // true if error { return m_val == 0 ? 0 : unspecified_bool_true; } </pre><p> It seems to me we can close this ticket now. </p> Ticket mgruenke@… Sun, 26 Aug 2012 23:19:43 GMT <link>https://svn.boost.org/trac10/ticket/5357#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5357#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/5357#comment:1" title="Comment 1">viboes</a>: </p> <blockquote class="citation"> <p> The code I'm seen in is </p> </blockquote> <p> Yes, and that's the problem. </p> <blockquote class="citation"> <p> It seems to me we can close this ticket now. </p> </blockquote> <p> Why is that? The problem has not been addressed. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 09 Sep 2012 18:03:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5357#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5357#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/5357#comment:2" title="Comment 2">mgruenke@…</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/5357#comment:1" title="Comment 1">viboes</a>: </p> <blockquote class="citation"> <p> The code I'm seen in is </p> </blockquote> <p> Yes, and that's the problem. </p> <blockquote class="citation"> <p> It seems to me we can close this ticket now. </p> </blockquote> <p> Why is that? The problem has not been addressed. </p> </blockquote> <p> Apologies. I didn't understood that the above code was related to a change proposal. </p> <p> IIUC, you pretend that the proposed change behavior is compatible with the C++11 standard. I don't think this is the case. </p> <p> A generic library using the current error_code behavior would break when instantiated with an error_category that refines the is_error behavior. So that this is a breaking change that needs more careful documentation. </p> <p> I will suggest you to post to the std-discussion ML this possible enhancement. </p> </description> <category>Ticket</category> </item> <item> <author>mgruenke@…</author> <pubDate>Sun, 09 Sep 2012 23:12:50 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5357#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5357#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/5357#comment:3" title="Comment 3">viboes</a>: </p> <blockquote class="citation"> <p> IIUC, you pretend that the proposed change behavior is compatible with the C++11 standard. I don't think this is the case. </p> </blockquote> <p> Pretend is a strong word. In fact, I made no statement about C++11. </p> <p> On that subject, I think Boost is Boost and the standard library is the standard library. I don't see why Boost can't offer more than what's provided by the standard library, even if it was influenced by a corresponding feature in Boost. </p> <blockquote class="citation"> <p> A generic library using the current error_code behavior would break when instantiated with an error_category that refines the is_error behavior. </p> </blockquote> <p> How would it break? A given bit of code is either using a boost::system::error_code or a std::error_code. They're different types, and whichever is being used, the compiler will correctly instantiate the corresponding check. </p> <blockquote class="citation"> <p> I will suggest you to post to the std-discussion ML this possible enhancement. </p> </blockquote> <p> Last I checked, this was the boost library bug tracker. If the standard library adopted this problematic behavior, I think that's unfortunate but probably too late to change. </p> <p> However, so long as there's boost::system, an option could exist for those cases where success does not map uniquely to 0. That's why I think this is worth fixing, regardless of what's in the standard library. </p> </description> <category>Ticket</category> </item> </channel> </rss>