stdbool.h中#define的基本原理为1 [英] rationale for #define true 1 in stdbool.h

查看:118
本文介绍了stdbool.h中#define的基本原理为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,制作内置类型_Bool的理由是什么,然后



#define true 1

#define false 0
stdbool.h中的
?这似乎很奇怪,真假都没有类型_Bool。

特别是我正在寻找一些语言扩展到C和一个

的最明显的扩展是重载。因为真实没有

类型_Bool它使_Bool的重载行为非常奇怪。你会认为

至少它可以是

#define true((bool)1)


我注意到了在C99规范中,它表示true和false定义适合在#if预处理器指令中使用
。是否预计true和

false将主要用于#if指令?可以想象一个

更重要的属性就像sizeof(bool)==

sizeof(true)。


谢谢,

-Ben

I''m curious, what was the rationale for making a builtin type _Bool but then
having
#define true 1
#define false 0
in stdbool.h? That seems very odd that true and false don''t have type _Bool.
In particular I''m poking around with some language extensions to C and one
of the most obvious extensions is overloading. Since "true" doesn''t have
type _Bool it makes overloading behavior with _Bool very odd. You''d think
that at least it could be
#define true ((bool)1)

I notice in the C99 spec it says the true and false defines "are suitable
for use in #if preprocessor directives". Was it anticipated that true and
false would be used primarily for #if directives? One would imagine that a
more important property would be something like sizeof(bool) ==
sizeof(true).

thanks,
-Ben

推荐答案




Ben Hinkle写道01/10/06 16:18,:


Ben Hinkle wrote On 01/10/06 16:18,:
我很好奇,制作内置类型_Bool的理由是什么,但后来
#define true 1 <在stdbool.h中#define false 0
?这似乎很奇怪,真假都没有类型_Bool。
特别是我正在寻找C语言的一些语言扩展,其中一个最明显的扩展是超载。因为真实没有
类型_Bool它使_Bool的重载行为非常奇怪。你会认为
至少它可能是#define true((bool)1)

我注意到在C99规范中它说明了真假定义 ;适合在#if预处理程序指令中使用
。是否预计true和
false将主要用于#if指令?可以想象一个更重要的属性就像sizeof(bool)==
sizeof(true)。
I''m curious, what was the rationale for making a builtin type _Bool but then
having
#define true 1
#define false 0
in stdbool.h? That seems very odd that true and false don''t have type _Bool.
In particular I''m poking around with some language extensions to C and one
of the most obvious extensions is overloading. Since "true" doesn''t have
type _Bool it makes overloading behavior with _Bool very odd. You''d think
that at least it could be
#define true ((bool)1)

I notice in the C99 spec it says the true and false defines "are suitable
for use in #if preprocessor directives". Was it anticipated that true and
false would be used primarily for #if directives? One would imagine that a
more important property would be something like sizeof(bool) ==
sizeof(true).




适合用于#if是一个原因(布尔)1不会b
工作。当预处理器运行时,类型尚不存在,

因此无法评估强制转换。 (事实上​​,#if true会将

转换为#if(bool)1然后#if(0)1,从而引出诊断。)


至于投诉的大小,虽然意见明显不同,但它不会让我感到重要。属性。恕我直言

通常 - 并不总是,但通常 - 当sizeof * ptr实用时,写一个sizeof(类型)是个坏主意。此外,

我们已经有了尺寸''x''> sizeof(char)在大多数系统上,

和唯一似乎打扰的人是

Dark Side With The Plus Signs的叛逃者。

就个人而言,我仍然不明白将

添加到语言的动机。理由引起人们对_Bool的一些属性的关注,但是没有说明为什么那些

属性如此可取以促使增加一个

全新的类型 - 特别是因为所有可以用_Bool完成的
在没有它的情况下显然是可行的。也许

C9X委员会遭遇Pascal嫉妒?


-
Er ********* @sun.com



"Suitable for use in #if" is one reason (bool)1 wouldn''t
work. Types do not yet exist when the preprocessor operates,
so casts can''t be evaluated. (In fact, #if true would turn
into #if (bool)1 and then #if (0)1, eliciting a diagnostic.)

As for the sizeof complaint, although opinions obviously
vary it doesn''t strike me as an "important" property. IMHO
it is usually -- not always, but usually -- a poor idea to
write sizeof(type) when sizeof *ptr is practical. Besides,
we''ve already got sizeof ''x'' > sizeof(char) on most systems,
and the only people it seems to bother are defectors to the
Dark Side With The Plus Signs.

Personally, I still don''t understand the motivation for
adding _Bool to the language. The Rationale draws attention
to some properties of _Bool, but sheds no light on why those
properties were so desirable as to prompt the addition of a
whole new type -- especially since everything that can be
done with _Bool seems eminently do-able without it. Perhaps
the C9X committee suffered from Pascal envy?

--
Er*********@sun.com


Eric Sosman写道:< br>
Eric Sosman wrote:
就我个人而言,我仍然不明白将添加到语言中的动机。
Personally, I still don''t understand the motivation for
adding _Bool to the language.




For我,这是可取的,因为为它分配任何非零值

会导致它具有非零值。对于

,除了unsigned long long之外,任何内置类型都不是这样,如果它被用作布尔类型,那将会浪费内存。


我不小心写了这样的代码:


bool b =(flags& FLAG_FOO);


其中FLAG_FOO类似于0x100。花了很长时间的b / b
调试会话来追踪问题;甚至

当我把问题隔离到这一段代码时,

我仍​​然不能为我的生活弄清楚发生了什么,

直到我查找''bool''的定义。 (结果是

是unsigned char的typedef)。



For me, it''s desirable because assigning any non-zero value
to it causes it to have a non-zero value. This is not true for
any builtin type except for unsigned long long, which would
be a waste of memory if it were used as a boolean type.

I have accidentally written code like this:

bool b = (flags & FLAG_FOO);

where FLAG_FOO is something like 0x100. It took a
long debugging session to track down the problem; even
when I''d isolated the problem to this one block of code,
I still couldn''t for the life of me figure out what was going on,
until I looked up the definition of ''bool''. (It turned out to
be a typedef for unsigned char).


" Ben Hinkle" < BH ***** @ mathworks.com>写道:
"Ben Hinkle" <bh*****@mathworks.com> writes:
我很好奇,制作内置类型_Bool的理由是什么,但后来
#define是真的1
#define false 0
在stdbool.h中?这似乎很奇怪,真假都没有类型_Bool。
特别是我正在寻找C语言的一些语言扩展,其中一个最明显的扩展是超载。因为真实没有
类型_Bool它使_Bool的重载行为非常奇怪。你会认为
至少它可能是#define true((bool)1)

我注意到在C99规范中它说明了真假定义 ;适合在#if预处理程序指令中使用
。是否预计true和
false将主要用于#if指令?可以想象一个更重要的属性就像sizeof(bool)==
sizeof(true)。
I''m curious, what was the rationale for making a builtin type _Bool but then
having
#define true 1
#define false 0
in stdbool.h? That seems very odd that true and false don''t have type _Bool.
In particular I''m poking around with some language extensions to C and one
of the most obvious extensions is overloading. Since "true" doesn''t have
type _Bool it makes overloading behavior with _Bool very odd. You''d think
that at least it could be
#define true ((bool)1)

I notice in the C99 spec it says the true and false defines "are suitable
for use in #if preprocessor directives". Was it anticipated that true and
false would be used primarily for #if directives? One would imagine that a
more important property would be something like sizeof(bool) ==
sizeof(true).




字符常量不要也没有char型;他们是int类型

(sizeof('''')== sizeof(int))。


制作真假类型_Bool不会非常有用,因为

无论如何它们都会在大多数情况下被提升为int。


如果语言已被更改因此所有条件必须是_B $ b类型_Bool,而不是任何标量类型,使得虚假和真实是_Bool类型_Bool可能更有意义 - 但是有点变化

会破坏现有代码。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< * GT; < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Character constants don''t have type char either; they''re of type int
(sizeof(''a'')==sizeof(int)).

Making true and false be of type _Bool wouldn''t be very useful, since
they''d be promoted to int in most contexts anyway.

If the language had been changed so that all conditions must be of
type _Bool, rather than of any scalar type, making false and true be
of type _Bool might have made more sense -- but that kind of change
would break existing code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于stdbool.h中#define的基本原理为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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