comp.lang.c++.moderated 上面有一篇帖子,说的是下面一段程序

为什么当OK宏未定义的时候,显示的结果如下:

UNSigned types:

unsigned __int64 is UNsigned

unsigned long is UNsigned

unsigned int is UNsigned

unsigned short is signed

unsigned char is signed

Signed types:

__int64 is signed

long is signed

int is signed

short is signed

signed char is signed

其中无符号的16位整型和无符号字符型都显示为有符号的了。而OK宏被定义的时候,却是正常的。作者很纳闷。为啥呢?

其实,作者此处绕了很多弯子,去用0和1做减法,看结果的符号来判断结果的类型,其实完全没有必要这么做,直接吧-1转换为目标类型,然后和零比较即可。而他认为能正常工作的,其实也只不过是在减法之后,又转换为目标类型了罢了。

现在来讨论 static const bool value = ((TIntegral)0) – ((TIntegral)1) > ((TIntegral)

0) ? false : true; 不能正常工作的原因吧,因为任何小于int的类型在计算时都已经提升为int了。呜呜。2008年过完了~

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>