Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Why bool is built-in data type (http://www.go4expert.com/articles/bool-built-data-type-t16392/)

asadullah.ansari 2Mar2009 13:55

Why bool is built-in data type


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

typedef int bool ;
It works fine but problem is that if someone overloads functions then it will produce problem like

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


enum bool

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


int i = 4;
if( i )

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


class bool
          operator int () {;} 
            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.

shabbir 2Mar2009 14:58

Re: Why bool is built-in data type
Nicely explained

Also have read the same in " ANSI/ISO C++ Professional Programmer's Handbook "

shabbir 6Apr2009 15:03

Re: Why bool is built-in data type
Article of the month competition nomination started here

zamjad 19May2009 23:30

Re: Why bool is built-in data type
Please take a look at the following

Do we really need a builtin bool type? Why not just emulate it in the existing language?

talk2mohdsaif 15Nov2009 18:28

Re: Why bool is built-in data type
very nice explaination ...
keep it up.....

All times are GMT +5.5. The time now is 04:19.