Boost C++ Libraries: Ticket #13120: optional o<size_t>. x<(size_t)0 is different to x<0 https://svn.boost.org/trac10/ticket/13120 <p> This is a very subtle but key bug. Comparing slightly different types changes the way the comparison is done. </p> <pre class="wiki">#include &lt;boost/optional.hpp&gt; #include &lt;cassert&gt; int main() { boost::optional&lt;size_t&gt; x; assert((x &lt; (size_t)0) == (x &lt; 0)); return 0; } </pre><p> result: </p> <pre class="wiki">$ g++ -g -I boost_1_64_0/ -o test_optional-d test_optional.cpp $ ./test_optional-d $ test_optional.cpp:7: int main(): Assertion `(x &lt; (size_t)0) == (x &lt; 0)' failed. </pre><p> I am scared to think what this might affect in my code... </p> <p> --- </p> <p> I would actually prefer to turn off the implicit cast in operator&lt;(), and only allow : optional&lt;T&gt; &lt; optional&lt;T&gt; </p> <p> I'd rather it throw a compile error and instead force me to do something like if (x &amp;&amp; *x &lt; 0) </p> <p> Because I do not always thing 'none' should be 'less than' anything. It should not be comparable. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13120 Trac 1.4.3 harris.pc@… Wed, 12 Jul 2017 15:45:59 GMT <link>https://svn.boost.org/trac10/ticket/13120#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13120#comment:1</guid> <description> <p> Note that I am using gcc 4.8.4 MSVC 2010 doesn't allow (x &lt; 0) to compile, complaining about the types not matching. </p> </description> <category>Ticket</category> </item> </channel> </rss>