Is 'bool' a basic datatype in C++?

前端 未结 7 1495
暖寄归人
暖寄归人 2020-12-02 13:02

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 doub

7条回答
  •  臣服心动
    2020-12-02 13:31

    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
    };
    

    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.

提交回复
热议问题