臭虫BOOST_CHECK? [英] Bug in BOOST_CHECK?

查看:409
本文介绍了臭虫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(它是C,而C ++颇为相似)定义6.3.1.8常见算术转换:

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中的int64_t 具有相同的等级,因此,这两个值都转换为 uint64_t中,和前pression等同于(uint64_t中)来源!=(uint64_t中)目标

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

这篇关于臭虫BOOST_CHECK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆