Can a bool variable store more than 0x01?

后端 未结 6 1127
慢半拍i
慢半拍i 2021-01-21 09:48
#include 
#include 

using namespace std;

int main()
{
    bool a = 0x03;
    bitset<8> x(a);
    cout<

        
6条回答
  •  孤城傲影
    2021-01-21 10:12

    A bool can only hold two values: false and true.

    When/if used in an integer context, a bool can be converted to an int. In this case, false converts to 0 and true converts to 1.

    Regardless of the size of storage used for a bool (e.g., sizeof(bool)==1 and sizeof(bool)==4 are both fairly common) it can still only hold the two values false and true, which always convert to 0 and 1 respectively. No other value is possible.

提交回复
热议问题