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

前端 未结 7 1480
暖寄归人
暖寄归人 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:34

    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.

    if(someval == true){
    

    probably translates to:

    if(someval !== false){ // e.g. someval !== 0
    

    by the compiler

提交回复
热议问题