Opened 5 years ago
Last modified 5 years ago
#13120 new Bugs
optional o<size_t>. x<(size_t)0 is different to x<0
Reported by: | Owned by: | Fernando Cacciola | |
---|---|---|---|
Milestone: | To Be Determined | Component: | optional |
Version: | Boost 1.64.0 | Severity: | Showstopper |
Keywords: | Cc: |
Description
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.
Note:
See TracTickets
for help on using tickets.
Note that I am using gcc 4.8.4 MSVC 2010 doesn't allow (x < 0) to compile, complaining about the types not matching.