问题描述
另请参见http://social.msdn.microsoft.com/Forums/zh-CN/vclanguage/thread/9c0b240a-4122-4d33-a7b3-9a7549adf0e7/ 上面有错误标记的解决方案.
See Also http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/9c0b240a-4122-4d33-a7b3-9a7549adf0e7/ which has a solution falsely marked on it.
有人可以告诉我为什么十年以后,编译器仍然不知道如何正确处理带符号/无符号的比较,必须让开发人员为他们处理吗?这是为解决问题而提出的一组宏,它们变成了 使用起来很丑,因为它们使您重新指定类型(编译器已经可以知道的东西)
Can someone tell me why after a decade plus, compilers still can't figure out how to correctly handle signed/usigned comparisons, and must make the developer handle it for them? Here are set of proposed macros to solve the problem, they become ugly in use, becuase they make you re-specify the type (something the compiler would already be able to know)
是要比较的值,at和bt是类型,而b类型是要比较的值的类型.是的,在某些情况下,不使用一种或另一种,但是为了一致性,我同时提供了两者.
in the macros below a and b are the values to compare, at and bt are a type and b type that is the types of the values to compare. Yes in certain cases one or the other type is not used, but for consistancy I supply both...
------------------
---------------
#ifdef _MSC_VER
#define COMPILER_THROWS_SIGNED_UNSIGNED_MISMATCH
#endif
#ifdef _MSC_VER
#define COMPILER_THROWS_SIGNED_UNSIGNED_MISMATCH
#endif
#ifdef COMPILER_THROWS_SIGNED_UNSIGNED_MISMATCH
#ifdef COMPILER_THROWS_SIGNED_UNSIGNED_MISMATCH
#define SUS_GT(a,at,b,bt) ((((a)< 0)?0:((((bt)a)>(b))))
#define USS_GT(a,at,b,bt) ((((b)< 0)?1:(((a)>((at)b)))
#define SUS_GT(a,at,b,bt) (((a)<0)?0:(((bt)a)>(b)))
#define USS_GT(a,at,b,bt) (((b)<0)?1:((a)>((at)b)))
#define SUS_LT(a,at,b,bt) ((((a)< 0)?1:((((bt)a)<(b)))
#define USS_LT(a,at,b,bt) ((((b)< 0)?0:((a)<((at)b)))
#define SUS_LT(a,at,b,bt) (((a)<0)?1:(((bt)a)<(b)))
#define USS_LT(a,at,b,bt) (((b)<0)?0:((a)<((at)b)))
#define SUS_GTE(a,at,b,bt) ((((a)< 0)?0:((((bt)a)&== b)))
#define USS_GTE(a,at,b,bt) ((((b)< 0)?1:(((a)> =((at)b))))
#define SUS_GTE(a,at,b,bt) (((a)<0)?0:(((bt)a)>=(b)))
#define USS_GTE(a,at,b,bt) (((b)<0)?1:((a)>=((at)b)))
#define SUS_LTE(a,at,b,bt) ((((a)< 0)?1:((((bt)a)< =(b)))
#define USS_LTE(a,at,b,bt) ((((b)< 0)?0:(((a)< =((at)b))))
#define SUS_LTE(a,at,b,bt) (((a)<0)?1:(((bt)a)<=(b)))
#define USS_LTE(a,at,b,bt) (((b)<0)?0:((a)<=((at)b)))
#else
#定义SUS_GT(a,at,b,bt) ((a)>(b))
#define USS_GT(a,at,b,bt) ((a)>(b))
#else
#define SUS_GT(a,at,b,bt) ((a)>(b))
#define USS_GT(a,at,b,bt) ((a)>(b))
#define SUS_LT(a,at,b,bt) ((a)<(b))
#define USS_LT(a,at,b,bt) ((a)<(b))
#define SUS_LT(a,at,b,bt) ((a)<(b))
#define USS_LT(a,at,b,bt) ((a)<(b))
#define SUS_GTE(a,at,b,bt) ((a)> =(b))
#define USS_GTE(a,at,b,bt) ((a)> =(b))
#define SUS_GTE(a,at,b,bt) ((a)>=(b))
#define USS_GTE(a,at,b,bt) ((a)>=(b))
#define SUS_LTE(a,at,b,bt) ((a)< =(b))
#define USS_LTE(a,at,b,bt) ((a)< =(b))
#endif
#define SUS_LTE(a,at,b,bt) ((a)<=(b))
#define USS_LTE(a,at,b,bt) ((a)<=(b))
#endif
-------------------
-------------------
SUS_是未签名的
USS_是未签名的
_GT大于
_LT小于
_GTE大于或等于
_LTE小于或等于
为什么它不给我有关有符号/无符号相等的警告(嗯,也许我不再有那些符号了)?我的意思是
and why doesn't it give me warnings about signed/unsigned equality (hmm maybe I don't have any of those anymore)? I mean
-3L == 0xFFFFFFFDUL 也不对.
-3L == 0xFFFFFFFDUL isn't right either.
------------------
---------------
但实际上-为什么我必须手动执行此操作?
But really - why should I have to do this manually?
有时-与您一起使用的API给您提供了必须解决的冲突类型...强制转换是可怕的,因为当类型更改并且事物迁移时,很多年都看不到问题了.甚至我的宏都不完整,因为从技术上讲,我应该 测试传递给"at"和"bt"的类型(a类型和b类型)实际上分别是a和b的类型.
And sometimes - the API you work with gives you conflicting types that you must resolve... casts are horrible, because when types change and things migrate, problems go unseen for many year. Even my macros are incomplete, because technically I should test that the types passed for 'at' and 'bt' (a-type and b-type) actually are the types of a and b respectively.
推荐答案
每次您进行比较时,都将要求编译器对数字进行假设.代替程序员明确说明,编译器只能猜测您的意图,并且您可能不会在意结果. (至少不是全部 时间.)
It would require the compiler to make an assumption about the numbers every time you made a comparison. In lieu of the programmer making it clear, the compiler can only guess your intent, and you would likely not care for the result. (At least not all the time.)
这篇关于为什么我必须处理有符号/无符号比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!