阅读背景:

C99 -为什么false和true被定义为0和1,而不是(bool)0和(bool)1 ?

来源:互联网 

Just stumbled across an assert, that failed, as it compared false to the returntype of a function, as the function itself returned a bool and the assert checked not only the value, but also the type of the returnvalue to match the one of false, to guarantee, that a bool is returned. Now the problem is, that C99 defines bool as _Bool and _Bool is even not necessarily same size as int (in fact, in my experience, on most platforms in nowadays it is often same size as unsigned char), not to talk about being same type (which is actually impossible, as _Bool is a builtin type of the language in C99), but defines false and true as 0 and 1 without any typecast and preprocessor definitions without a typecast will default to int. If C99 would instead define false and true as ((bool)0) and ((bool)1), they would always be of type bool, no matter, how _Bool is defined. So is there any good reason to have them always defined as ints, even when bool is not an int on that platform or is this just a bug in the language, that should be fixed with C1x?Just stumbled across an assert, that failed, as




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: