'bool' 是 C++ 中的基本数据类型吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/356726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is 'bool' a basic datatype in C++?
提问by Naveen
I got this doubt while writing some code. Is 'bool' a basic datatype defined in the C++ standard or is it some sort of extension provided by the compiler ? I got this doubt because Win32 has 'BOOL' which is nothing but a typedef of long. Also what happens if I do something like this:
我在编写一些代码时遇到了这个疑问。'bool' 是 C++ 标准中定义的基本数据类型还是编译器提供的某种扩展?我有这个疑问,因为 Win32 有 'BOOL',它只是一个 long 的 typedef。如果我做这样的事情会发生什么:
int i = true;
Is it "always" guaranteed that variable i will have value 1 or is it again depends on the compiler I am using ? Further for some Win32 APIs which accept BOOL as the parameter what happens if I pass bool variable?
是“始终”保证变量 i 的值为 1 还是再次取决于我使用的编译器?此外,对于一些接受 BOOL 作为参数的 Win32 API,如果我传递 bool 变量会发生什么?
回答by Johannes Schaub - litb
bool is a fundamental datatype in C++. Converting true
to an integer type will yield 1, and converting false
will yield 0 (4.5/4 and 4.7/4). In C, until C99, there was no bool datatype, and people did stuff like
bool 是 C++ 中的基本数据类型。转换true
为整数类型将产生 1,转换false
将产生 0(4.5/4 和 4.7/4)。在 C 中,直到 C99,没有 bool 数据类型,人们做了类似的事情
enum bool {
false, true
};
So did the Windows API. Starting with C99, we have _Bool
as a basic data type. Including stdbool.h
will typedef#define
that to bool
and provide the constants true
and false
. They didn't make bool a basic data-type (and thus a keyword) because of compatibility issues with existing code.
Windows API 也是如此。从 C99 开始,我们有_Bool
一个基本的数据类型。包括stdbool.h
将typedef#define
that tobool
并提供常量true
和false
. 由于与现有代码的兼容性问题,他们没有使 bool 成为基本数据类型(因此是关键字)。
回答by jalf
Yes, bool is a built-in type.
是的, bool 是一种内置类型。
WIN32 is C code, not C++, and C does not have a bool, so they provide their own typedef BOOL.
WIN32是C代码,不是C++,C没有bool,所以他们提供了自己的typedef BOOL。
回答by hazzen
C++ does lots of automatic casting for you - that is, if you have a variable of type bool
and pass it to something expecting an int
, it will make it into an int
for you - 0
for false
and 1
for true
.
C++ 为你做了很多自动转换——也就是说,如果你有一个类型的变量bool
并将它传递给期待 an 的东西int
,它会int
为你把它变成 an - 0
forfalse
和1
for true
。
I don't have my standard around to see if this is guaranteed, but every compiler I've used does this (so one can assume it will always work).
我没有我的标准来看看这是否有保证,但我使用过的每个编译器都这样做(所以人们可以假设它总是有效)。
However, relying on this conversion is a bad idea. Code can stop compiling if a new method is added that overloads the int
signature, etc.
但是,依赖这种转换是一个坏主意。如果添加了重载int
签名等的新方法,代码可以停止编译。
回答by John T
回答by Anish
Turbo c and c++ compiler does not support boolean (bool keyword) data type but dev c++ compiler supports boolean (bool keyword) data type.
Turbo c 和 c++ 编译器不支持 boolean(bool 关键字)数据类型,但 dev c++ 编译器支持 boolean(bool 关键字)数据类型。
回答by jdmichal
C is meant to be a step above assembly language. The C if-statement is really just syntactical sugar for "branch-if-zero", so the idea of booleans as an independent datatype was a foreign concept at the time. (1)
C 意味着比汇编语言高出一步。C if 语句实际上只是“如果零分支”的语法糖,因此布尔值作为独立数据类型的想法在当时是一个陌生的概念。(1)
Even now, C/C++ booleans are usually little more than an alias for a single byte data type. As such, it's really more of a purposing label than an independent datatype.
即使是现在,C/C++ 布尔值通常也只是单字节数据类型的别名。因此,它实际上更像是一个有目的的标签,而不是一个独立的数据类型。
(1) Of course, modern compilers are a bit more advanced in their handling of if statements. This is from the standpoint of C as a new language.
(1) 当然,现代编译器在处理 if 语句方面要先进一些。这是从 C 作为一种新语言的角度来看的。
回答by Stein G. Strindhaug
Allthough it's now a native type, it's still defined behind the scenes as an integer (int I think) where the literal false
is 0 and true
is 1. But I think all logic still consider anything but 0 as true, so strictly speaking the true literal is probably a keyword for the compiler to test if something is not false.
虽然它现在是原生类型,但它仍然在幕后被定义为一个整数(我认为false
是int),其中文字为 0 和true
1。但我认为所有逻辑仍然认为除 0 外的任何东西都是真的,所以严格来说,真正的文字是可能是编译器测试某些内容是否正确的关键字。
if(someval == true){
probably translates to:
大概翻译成:
if(someval !== false){ // e.g. someval !== 0
by the compiler
由编译器