Why bool is built-in data type

asadullah.ansari's Avatar author of Why bool is built-in data type
This is an article on Why bool is built-in data type in C.
Rated 5.00 By 1 users

Introduction



After long day's discussion, bool was made as built-in type. Before it lot of other proposals came for bool. There were following proposals.

Proposal 1: bool can be implemented by typedef
Code:
typedef int bool ;
It works fine but problem is that if someone overloads functions then it will produce problem like
Code:
void Fun( int );
void Fun( bool);
When you will this function by Fun(2), then ambiguity will come. So this proposal is
dropped. I don't think so it gives any problem in C. Only problem will be in C++. Let's waiting for you guys comments.

Proposal 2: bool can be implemented by enumeration (enum)

This proposal was based on enum like

Code:
enum bool
{
      false,
      true
};
it would work fine but what's the problem. You can find the problem by using bool as enum , suppose you are using if condition

Code:
int i = 4;
if( i )
{
      //statement
}
Since enum is strongly typed so in if condition only bool should take. But more flexibility should be given to programmer. It will be useless if in if condition does not work for other type like integer. So this proposal was dropped.

Proposal 3: Bool can be implemented as class

Code:
class bool
  {
         public:
           operator int () {;}  
         private:
            int bVal;
   };
Now this proposal was okay. Why standard committee didn't approved it. Class always gives unique type? So what was the problem in that? A lot of problem it has

1. Since bool is class so whenever you will take a variable of bool two functions one is Constructor and other is destructor. So it's pain, costly.
2. One thing more whenever you want to use you have include this files to your working codes/projects/program etc. From my point of view it's not a problem because by any way you are including a lot of files or this bool can be declared already existing library only.
3. This is the last drawback of bool using as class is that the conversion operator might interfere with user-defined conversion operators that are defined in other classes. This is more important drawback not to use bool as class.

So seeing above drawbacks, standard committee decided to make bool as built-in type. So bool as built-in type gives us better readability, good performance, type distinctness, portability etc.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nicely explained

Also have read the same in " ANSI/ISO C++ Professional Programmer's Handbook "
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Article of the month competition nomination started here
0
zamjad's Avatar, Join Date: Oct 2008
Go4Expert Member
Please take a look at the following

Do we really need a builtin bool type? Why not just emulate it in the existing language?
http://www.gotw.ca/gotw/026.htm
0
talk2mohdsaif's Avatar, Join Date: Mar 2009
Go4Expert Member
very nice explaination ...
keep it up.....