[Boost-bugs] [Boost C++ Libraries] #13120: optional o<size_t>. x<(size_t)0 is different to x<0

Subject: [Boost-bugs] [Boost C++ Libraries] #13120: optional o<size_t>. x<(size_t)0 is different to x<0
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2017-07-12 08:13:11


#13120: optional o<size_t>. x<(size_t)0 is different to x<0
------------------------------+-------------------------------
 Reporter: harris.pc@… | Owner: Fernando Cacciola
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: optional
  Version: Boost 1.64.0 | Severity: Showstopper
 Keywords: |
------------------------------+-------------------------------
 This is a very subtle but key bug.
 Comparing slightly different types changes the way the comparison is done.

 {{{
 #include <boost/optional.hpp>
 #include <cassert>

 int main()
 {
    boost::optional<size_t> x;
    assert((x < (size_t)0) == (x < 0));
    return 0;
 }
 }}}

 result:
 {{{
 $ 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 < (size_t)0) == (x < 0)'
 failed.
 }}}

 I am scared to think what this might affect in my code...

 ---

 I would actually prefer to turn off the implicit cast in operator<(),
 and only allow : optional<T> < optional<T>

 I'd rather it throw a compile error and instead force me to do something
 like
 if (x && *x < 0)

 Because I do not always thing 'none' should be 'less than' anything. It
 should not be comparable.

-- 
Ticket URL: <https://svn.boost.org/trac10/boost/ticket/13120>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-07-12 08:17:48 UTC