Why bool is built-in data type

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

  1. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA

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

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Nicely explained

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

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Article of the month competition nomination started here
     
  4. zamjad

    zamjad New Member

    Joined:
    Oct 7, 2008
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    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
     
  5. talk2mohdsaif

    talk2mohdsaif New Member

    Joined:
    Mar 8, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Hamirpur(hp)
    very nice explaination ...
    keep it up.....
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice