1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Why bool is built-in data type

Discussion in 'C' started by asadullah.ansari, Mar 2, 2009.

  1. asadullah.ansari

    asadullah.ansari TechCake


    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.
  2. shabbir

    shabbir Administrator Staff Member

    Nicely explained

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

    shabbir Administrator Staff Member

    Article of the month competition nomination started here
  4. zamjad

    zamjad New 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?
  5. talk2mohdsaif

    talk2mohdsaif New Member

    very nice explaination ...
    keep it up.....

Share This Page