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

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

    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.

    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.

提交回复
热议问题