“布尔”是C ++中的基本数据类型吗? [英] Is 'bool' a basic datatype in C++?

查看:195
本文介绍了“布尔”是C ++中的基本数据类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写一些代码时有这个疑问。 布尔是C ++标准中定义的基本数据类型,还是编译器提供的某种扩展?我对此表示怀疑,因为Win32的 BOOL只是long的typedef。如果我做这样的事情,也会发生什么事:

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:

int i = true;

是否总是保证变量i的值为1还是再次取决于编译器我在用 ?进一步对于某些接受BOOL作为参数的Win32 API,如果我传递bool变量会发生什么情况?

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?

推荐答案

bool是其中的基本数据类型C ++。将 true 转换为整数类型将产生1,而将 false 转换为整数(4.5 / 4和4.7 / 4 )。在C语言中,直到C99都没有bool数据类型,人们所做的事情是

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

enum bool {
    false, true
};

Windows API也是如此。从C99开始,我们将 _Bool 作为基本数据类型。包括 stdbool.h typedef #define 变为 bool 并提供常量 true false 。由于现有代码的兼容性问题,他们没有将bool设为基本的数据类型(因此也没有成为关键字)。

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.

这篇关于“布尔”是C ++中的基本数据类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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