The bool
type is one of the c++ fundamental data type , a fundamental data type has a cpu hardware mapping .
The bool
type is used for boolean values . A boolean value is the result of a relational operation , such as a comparison for equality ==
, or a logical operation , such as or ||
.
The result of a boolean operation , can either be true or false , hence C++ introduced the two keywords : true
and false
. true
and false
in C++ , are the boolean literals .
The C++ standard specifies , that the bool
type , must have the same size , alignment and bit representation , as an unsigned integer type . Typically C++ implementations define the bool
type , as having a size of 1 byte , but this is not mandated by the standard .
#include<iostream> int main(void ){ std::cout << sizeof(bool ) << " byte ." << std::endl ; /*Print the size of the bool type , on this machine . Output : 1 byte .*/ }
When performing flow control statements , such as if
or while
, the conditional expression that is being evaluated , has a resulting type of bool
. The only exception is the switch
statement , which has a resulting integral type .
Integer types such as int
, floating point types such as float
, pointers , and enumerations , can be converted to a boolean value , where 0
or the null pointer is false , and everything else is true .
#include<iostream> int main(void ){ enum state{halt , run }; /*Define the enum state , which has two state , halt which is 0 , and run which is 1 .*/ int var_i = 1 ; int *ptr_i = nullptr; if(!ptr_i && run ) /*if ptr_i , is the null pointer , , ptr_i evaluates to false , and not ptr_i evaluates to true . run has a value of 1 , as such it evaluates to true . Hence if ptr_is the null pointer and the state is run , ptr_i is initialized with the address of var_i .*/ ptr_i = &var_i ; while(ptr_i && *ptr_i ){ /*ptr_i evaluates to false , if ptr_i is the null pointer, and to true otherwise , *ptr_i evaluates to true if different from 0 , and to false otherwise . The while loop will continue , as long as ptr_i is not null , and *ptr_i is not 0 .*/ std::cout << *ptr_i << std::endl ; *ptr_i = *ptr_i - 1 ; } /*Output : 1 .*/ }
When the type of an expression is to be of an integer type , the bool
type can be converted to an integer type , where true is promoted , as in integer promotion , to 1
, and false is promoted to 0
.
#include<iostream> int main(void ){ switch(true ){ /*In a switch statement , the conditional expression is of an integer type , hence true is converted to 1 .*/ case 0 : std::cout << false << " : " << std::boolalpha << false ; break; case 1 : /*When converted to an integer type , true evaluates to 1 , and false , to 0 . case 1 , is the true case , hence it is the one , that is executed .*/ std::cout << true << " : "<<std::boolalpha << true <<std::endl; /*cout by default prints the integer value of a boolean type , so first it will print the integer value of true which is 1. When passed the option boolalpha , it will print the alpha value of the boolean type , hence next it prints true . Output : 1 : true .*/ break; } }
A class type : class
, struct
, union
, can be evaluated to a boolean if it provides an operator conversion function , and it can perform relational and logical operations , if it provides logical and relational operator functions .
#include<iostream> class Foo{ /*Class Foo Definition .*/ public : int var_i; bool operator==(const Foo foo ); bool operator!=(const Foo foo ); bool operator&&(const Foo foo ); operator bool( ); }; bool Foo::operator==(const Foo foo ){ /*Definition of the operator == function , that compares two instances of Foo for equality .*/ return var_i == foo.var_i ;} bool Foo::operator!=(const Foo foo ){ /*Definition of the operator != function , which compares two instances of Foo for difference .*/ return var_i != foo.var_i ;} bool Foo::operator&&(const Foo foo ){ /*Definition of operator && function , which implements the and logical operation between two instance of Foo .*/ switch(var_i - foo.var_i ){ /*subtract var_i from foo.var_i and return false , if the result of the subtraction is 0 , 1 otherwise .*/ case 0 : return false; default: return true; } } Foo::operator bool(){ /*Definition of the operator conversion to a bool data type function , which the result of its evaluation is a bool type .*/ switch(var_i ) { /*If var_i is equal to 0 , return false , otherwise return true .*/ case 0 : return false ; default : return true; } } int main(void ){ /*The main function .*/ Foo var1_foo{-1 }; Foo var2_foo{0 }; /*Define two objects var1_foo , and var2_foo , initializing each to -1 and 0 respectively .*/ if(var1_foo == var2_foo ) /*Compare var1_foo and var2_foo for equality . var1_foo operator function equal is called , being passed var2_foo as an argument . Their var_i data members are not equal , hence the operator function == returns false , the body of the if statement is not executed .*/ std::cout << "var1_foo is equal var2_foo ." << std::endl ; if(var1_foo != var2_foo ) /*Compare var1_foo and var2_foo for difference . The operator function != of var1_foo is called , being passed var2_foo as an argument . Their var_i data members are different , hence the result of the operator function != is true , hence the body of the if statement is executed .*/ std::cout << "var1_foo is different from var2_foo ." << std::endl ; /*Output : var1_foo is different from var2_foo .*/ if(var1_foo ) /*var1_foo is evaluated as a boolean , since the destination type for an if statement is a boolean type . var1_foo is converted to a boolean , by calling its operator conversion function bool , which switches the var_i data member , and return true if its different from 0 , false otherwise .*/ std::cout << "var1_foo is true ." << std::endl ; /*Output : var1_foo is true .*/ if(!var2_foo ) /*var2_foo is evaluated as a boolean , hence it is converted to a boolean , since the destination type of an if statement is the bool type . var2_foo conversion operator function bool is called , and it returns a bool , which is true if the var_i data member is different from 0 , and false otherwise .*/ std::cout << "var2_foo is false ." << std::endl ; /*Output : var2_foo is false .*/ if(var2_foo && var1_foo ) /*var2_foo operator function && is called , it is passed var1_foo as an argument . The operator function && subtract var2_foo var_i data member from var1_foo var_i data member : 0 - (-1 ) is equal to 1 , hence the operator function && returns a true , as such the body of the if statement is executed .*/ std::cout<< "var1_foo && var2_foo is true ." <<std::endl; /*Output : var1_foo && var2_foo is true .*/ }
The bool
type can be used as the return type of predicate functions , these are functions which must return either true
or false
.
bool isCppStandard_Compliant(){ return true ;}