Reputation: 24675
uint64_t source = numeric_limits<uint64_t>::max();
int64_t target = source;
BOOST_CHECK(source != target);//THIS SHOULD CHECK AS true - target != source
This check is failing but it should pass - source is different from target.
Upvotes: 1
Views: 268
Reputation: 41627
Yes, they are different, but when they are compared using !=
, the usual arithmetic conversions are applied to them. That means both values are converted to the same data type.
ISO C99 (it's for C, but C++ is quite similar) defines in 6.3.1.8 Usual arithmetic conversions:
[...] Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
uint64_t
and int64_t
have the same rank, so both values are converted to uint64_t
, and the expression is equivalent to (uint64_t) source != (uint64_t) target
.
To get the result you want, you could check source == target && (source < 0) == (target < 0)
.
Upvotes: 5