简体   繁体   中英

Bug in BOOST_CHECK?

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.

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM