rationale for #define true 1 in stdbool.h

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Hinkle

    rationale for #define true 1 in stdbool.h

    I'm curious, what was the rationale for making a builtin type _Bool but then
    having
    #define true 1
    #define false 0
    in stdbool.h? That seems very odd that true and false don't have type _Bool.
    In particular I'm poking around with some language extensions to C and one
    of the most obvious extensions is overloading. Since "true" doesn't have
    type _Bool it makes overloading behavior with _Bool very odd. You'd think
    that at least it could be
    #define true ((bool)1)

    I notice in the C99 spec it says the true and false defines "are suitable
    for use in #if preprocessor directives". Was it anticipated that true and
    false would be used primarily for #if directives? One would imagine that a
    more important property would be something like sizeof(bool) ==
    sizeof(true).

    thanks,
    -Ben


  • Eric Sosman

    #2
    Re: rationale for #define true 1 in stdbool.h



    Ben Hinkle wrote On 01/10/06 16:18,:[color=blue]
    > I'm curious, what was the rationale for making a builtin type _Bool but then
    > having
    > #define true 1
    > #define false 0
    > in stdbool.h? That seems very odd that true and false don't have type _Bool.
    > In particular I'm poking around with some language extensions to C and one
    > of the most obvious extensions is overloading. Since "true" doesn't have
    > type _Bool it makes overloading behavior with _Bool very odd. You'd think
    > that at least it could be
    > #define true ((bool)1)
    >
    > I notice in the C99 spec it says the true and false defines "are suitable
    > for use in #if preprocessor directives". Was it anticipated that true and
    > false would be used primarily for #if directives? One would imagine that a
    > more important property would be something like sizeof(bool) ==
    > sizeof(true).[/color]

    "Suitable for use in #if" is one reason (bool)1 wouldn't
    work. Types do not yet exist when the preprocessor operates,
    so casts can't be evaluated. (In fact, #if true would turn
    into #if (bool)1 and then #if (0)1, eliciting a diagnostic.)

    As for the sizeof complaint, although opinions obviously
    vary it doesn't strike me as an "important" property. IMHO
    it is usually -- not always, but usually -- a poor idea to
    write sizeof(type) when sizeof *ptr is practical. Besides,
    we've already got sizeof 'x' > sizeof(char) on most systems,
    and the only people it seems to bother are defectors to the
    Dark Side With The Plus Signs.

    Personally, I still don't understand the motivation for
    adding _Bool to the language. The Rationale draws attention
    to some properties of _Bool, but sheds no light on why those
    properties were so desirable as to prompt the addition of a
    whole new type -- especially since everything that can be
    done with _Bool seems eminently do-able without it. Perhaps
    the C9X committee suffered from Pascal envy?

    --
    Eric.Sosman@sun .com

    Comment

    • Old Wolf

      #3
      Re: rationale for #define true 1 in stdbool.h

      Eric Sosman wrote:
      [color=blue]
      > Personally, I still don't understand the motivation for
      > adding _Bool to the language.[/color]

      For me, it's desirable because assigning any non-zero value
      to it causes it to have a non-zero value. This is not true for
      any builtin type except for unsigned long long, which would
      be a waste of memory if it were used as a boolean type.

      I have accidentally written code like this:

      bool b = (flags & FLAG_FOO);

      where FLAG_FOO is something like 0x100. It took a
      long debugging session to track down the problem; even
      when I'd isolated the problem to this one block of code,
      I still couldn't for the life of me figure out what was going on,
      until I looked up the definition of 'bool'. (It turned out to
      be a typedef for unsigned char).

      Comment

      • Keith Thompson

        #4
        Re: rationale for #define true 1 in stdbool.h

        "Ben Hinkle" <bhinkle@mathwo rks.com> writes:[color=blue]
        > I'm curious, what was the rationale for making a builtin type _Bool but then
        > having
        > #define true 1
        > #define false 0
        > in stdbool.h? That seems very odd that true and false don't have type _Bool.
        > In particular I'm poking around with some language extensions to C and one
        > of the most obvious extensions is overloading. Since "true" doesn't have
        > type _Bool it makes overloading behavior with _Bool very odd. You'd think
        > that at least it could be
        > #define true ((bool)1)
        >
        > I notice in the C99 spec it says the true and false defines "are suitable
        > for use in #if preprocessor directives". Was it anticipated that true and
        > false would be used primarily for #if directives? One would imagine that a
        > more important property would be something like sizeof(bool) ==
        > sizeof(true).[/color]

        Character constants don't have type char either; they're of type int
        (sizeof('a')==s izeof(int)).

        Making true and false be of type _Bool wouldn't be very useful, since
        they'd be promoted to int in most contexts anyway.

        If the language had been changed so that all conditions must be of
        type _Bool, rather than of any scalar type, making false and true be
        of type _Bool might have made more sense -- but that kind of change
        would break existing code.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Ben Hinkle

          #5
          Re: rationale for #define true 1 in stdbool.h


          "Eric Sosman" <eric.sosman@su n.com> wrote in message
          news:43C42B88.1 000006@sun.com. ..[color=blue]
          >
          >
          > Ben Hinkle wrote On 01/10/06 16:18,:[color=green]
          >> I'm curious, what was the rationale for making a builtin type _Bool but
          >> then
          >> having
          >> #define true 1
          >> #define false 0
          >> in stdbool.h? That seems very odd that true and false don't have type
          >> _Bool.
          >> In particular I'm poking around with some language extensions to C and
          >> one
          >> of the most obvious extensions is overloading. Since "true" doesn't have
          >> type _Bool it makes overloading behavior with _Bool very odd. You'd think
          >> that at least it could be
          >> #define true ((bool)1)
          >>
          >> I notice in the C99 spec it says the true and false defines "are suitable
          >> for use in #if preprocessor directives". Was it anticipated that true and
          >> false would be used primarily for #if directives? One would imagine that
          >> a
          >> more important property would be something like sizeof(bool) ==
          >> sizeof(true).[/color]
          >
          > "Suitable for use in #if" is one reason (bool)1 wouldn't
          > work. Types do not yet exist when the preprocessor operates,
          > so casts can't be evaluated. (In fact, #if true would turn
          > into #if (bool)1 and then #if (0)1, eliciting a diagnostic.)[/color]

          Right. I wouldn't consider using true and false in #if's important.
          [color=blue]
          > As for the sizeof complaint, although opinions obviously
          > vary it doesn't strike me as an "important" property. IMHO
          > it is usually -- not always, but usually -- a poor idea to
          > write sizeof(type) when sizeof *ptr is practical. Besides,
          > we've already got sizeof 'x' > sizeof(char) on most systems,
          > and the only people it seems to bother are defectors to the
          > Dark Side With The Plus Signs.[/color]

          I'm with Them, then. Justifying one "mistake" (#define true 1) with another
          (type of 'a' isn't char) doesn't make me feel warm and fuzzy. I assume there
          are good reasons for things, though.
          [color=blue]
          > Personally, I still don't understand the motivation for
          > adding _Bool to the language. The Rationale draws attention
          > to some properties of _Bool, but sheds no light on why those
          > properties were so desirable as to prompt the addition of a
          > whole new type -- especially since everything that can be
          > done with _Bool seems eminently do-able without it. Perhaps
          > the C9X committee suffered from Pascal envy?[/color]

          Not enough Pascal envy, perhaps ;-)
          [color=blue]
          >
          > --
          > Eric.Sosman@sun .com
          >[/color]


          Comment

          • Peter Nilsson

            #6
            Re: rationale for #define true 1 in stdbool.h

            Eric Sosman wrote:[color=blue]
            >
            > Personally, I still don't understand the motivation for
            > adding _Bool to the language. The Rationale draws attention
            > to some properties of _Bool, but sheds no light on why those
            > properties were so desirable as to prompt the addition of a
            > whole new type -- especially since everything that can be
            > done with _Bool seems eminently do-able without it. Perhaps
            > the C9X committee suffered from Pascal envy?[/color]

            Or perhaps the C9X committee could see the plethora of
            programs that already have varing (and subtly incompatible)
            kludges for the same thing that was missing from C originally,
            namely, a basic boolean type.

            --
            Peter

            Comment

            • Eric Sosman

              #7
              Re: rationale for #define true 1 in stdbool.h

              Old Wolf wrote:
              [color=blue]
              > Eric Sosman wrote:
              >
              >[color=green]
              >> Personally, I still don't understand the motivation for
              >>adding _Bool to the language.[/color]
              >
              >
              > For me, it's desirable because assigning any non-zero value
              > to it causes it to have a non-zero value. This is not true for
              > any builtin type except for unsigned long long, which would
              > be a waste of memory if it were used as a boolean type.
              >
              > I have accidentally written code like this:
              >
              > bool b = (flags & FLAG_FOO);
              >
              > where FLAG_FOO is something like 0x100. It took a
              > long debugging session to track down the problem; even
              > when I'd isolated the problem to this one block of code,
              > I still couldn't for the life of me figure out what was going on,
              > until I looked up the definition of 'bool'. (It turned out to
              > be a typedef for unsigned char).[/color]

              Accidents will happen (and have to me, most certainly).
              This particular accident isn't one that has beset my path
              and laid a trap for my unwary feet; in the "isolate the
              bit" context I tend to think of (and write) the result in
              the same type as the original flags, not as a truth value
              saying "was it set, or was it not?"

              Still, if one wants the WISOWIN semantics, one can
              always resort to the double negative:

              bool b = !!(flags & FLAG_FOO);

              or the (possibly clearer)

              bool b = (flags & FLAG_FOO) != 0;

              Thirty-five-plus years of C somehow scraped by without
              anything more, and I still don't see what _Bool brings to
              the party. Well, maybe I'm just a party pooper.

              --
              Eric Sosman
              esosman@acm-dot-org.invalid

              Comment

              • Chris Torek

                #8
                Re: rationale for #define true 1 in stdbool.h

                >Eric Sosman wrote:[color=blue][color=green]
                >> Personally, I still don't understand the motivation for
                >> adding _Bool to the language. ...[/color][/color]

                In article <1136945432.540 216.58070@o13g2 000cwo.googlegr oups.com>
                Peter Nilsson <airia@acay.com .au> wrote:[color=blue]
                >Or perhaps the C9X committee could see the plethora of
                >programs that already have varing (and subtly incompatible)
                >kludges for the same thing that was missing from C originally,
                >namely, a basic boolean type.[/color]

                In other words, there was a demand, so they filled it.

                Of course, there is also a lot of demand for methamphetamine . :-)

                (Or as Rob Pike said of the X Window System: "Sometimes when you
                fill a vacuum, it still sucks.")
                --
                In-Real-Life: Chris Torek, Wind River Systems
                Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                email: forget about it http://web.torek.net/torek/index.html
                Reading email is like searching for food in the garbage, thanks to spammers.

                Comment

                • Chuck F.

                  #9
                  Re: rationale for #define true 1 in stdbool.h

                  Eric Sosman wrote:[color=blue]
                  >[/color]
                  .... snip ...[color=blue]
                  >
                  > Thirty-five-plus years of C somehow scraped by without anything
                  > more, and I still don't see what _Bool brings to the party.
                  > Well, maybe I'm just a party pooper.[/color]

                  You can always avoid it by simply failing to #include <stdbool.h>.
                  Then the only evidence remaining is an identifier in the
                  implementors namespace. But for people who do want to define true,
                  false, and bool, everything is standardized.

                  --
                  "If you want to post a followup via groups.google.c om, don't use
                  the broken "Reply" link at the bottom of the article. Click on
                  "show options" at the top of the article, then click on the
                  "Reply" at the bottom of the article headers." - Keith Thompson
                  More details at: <http://cfaj.freeshell. org/google/>

                  Comment

                  • Alex Fraser

                    #10
                    Re: rationale for #define true 1 in stdbool.h

                    "Keith Thompson" <kst-u@mib.org> wrote in message
                    news:ln1wzfa7h5 .fsf@nuthaus.mi b.org...
                    [snip][color=blue]
                    > Making true and false be of type _Bool wouldn't be very useful, since
                    > they'd be promoted to int in most contexts anyway.[/color]

                    Which leads to the obvious question: why are "small" types promoted to int?
                    [color=blue]
                    > If the language had been changed so that all conditions must be of
                    > type _Bool, rather than of any scalar type, making false and true be
                    > of type _Bool might have made more sense -- but that kind of change
                    > would break existing code.[/color]

                    I guess you would also want to change the type of the result of the equality
                    and relational operators.

                    Alex


                    Comment

                    • Keith Thompson

                      #11
                      Re: rationale for #define true 1 in stdbool.h

                      "Alex Fraser" <me@privacy.net > writes:[color=blue]
                      > "Keith Thompson" <kst-u@mib.org> wrote in message
                      > news:ln1wzfa7h5 .fsf@nuthaus.mi b.org...
                      > [snip][color=green]
                      >> Making true and false be of type _Bool wouldn't be very useful, since
                      >> they'd be promoted to int in most contexts anyway.[/color]
                      >
                      > Which leads to the obvious question: why are "small" types promoted to int?[/color]

                      Historical reasons and code efficiency, I think.

                      For many CPUs, arithmetic operations single-word operands are more
                      efficient than operations on smaller operands. Given:

                      short x, y, z;
                      ... x * y + z ...

                      promoting each operand from short (perhaps 16 bits) to int (perhaps 32
                      bits) is likely to result in faster code than performin the operations
                      on the short operands (which might not even be directly supported by
                      the hardware).

                      Making the semantics consistent across different CPUs is probably
                      better than doing it in the most efficient way for each system,
                      causing some apparently straightforward operations to be
                      implementation-defined.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • Ben Hinkle

                        #12
                        Re: rationale for #define true 1 in stdbool.h


                        "Chuck F. " <cbfalconer@yah oo.com> wrote in message
                        news:9sednefape aDPFneRVn-hg@maineline.ne t...[color=blue]
                        > Eric Sosman wrote:[color=green]
                        >>[/color]
                        > ... snip ...[color=green]
                        >>
                        >> Thirty-five-plus years of C somehow scraped by without anything
                        >> more, and I still don't see what _Bool brings to the party.
                        >> Well, maybe I'm just a party pooper.[/color]
                        >
                        > You can always avoid it by simply failing to #include <stdbool.h>. Then
                        > the only evidence remaining is an identifier in the implementors
                        > namespace. But for people who do want to define true, false, and bool,
                        > everything is standardized.[/color]

                        But if the intention is that stdbool be used for standard boolean coding
                        then it will appear in headers and once you include someone's header that
                        uses it then you are using it, too. So if the intention of stdbool is that
                        it is included in, say, 90% of headers that involve function taking a
                        boolean parameter then as an individual there's not much one can do to avoid
                        it. However if the intention is that it gets used by a few individuals (more
                        particuarly, not in library headers) then how it behaves isn't a big deal.

                        -Ben


                        Comment

                        • lawrence.jones@ugs.com

                          #13
                          Re: rationale for #define true 1 in stdbool.h

                          Eric Sosman <eric.sosman@su n.com> wrote:[color=blue]
                          >
                          > Personally, I still don't understand the motivation for
                          > adding _Bool to the language.[/color]

                          The main reason was that a huge number of third-party packages define
                          some sort of boolean type, but while they usually don't agree on the
                          definition, they frequently agree on the name, leading to problems for
                          anyone trying to use them together. Having a standard boolean type was
                          viewed as the obvious solution to that problem.

                          -Larry Jones

                          They say winning isn't everything, and I've decided
                          to take their word for it. -- Calvin

                          Comment

                          • lawrence.jones@ugs.com

                            #14
                            Re: rationale for #define true 1 in stdbool.h

                            Old Wolf <oldwolf@inspir e.net.nz> wrote:[color=blue]
                            >
                            > For me, it's desirable because assigning any non-zero value
                            > to it causes it to have a non-zero value. This is not true for
                            > any builtin type except for unsigned long long [...][/color]

                            unsigned long long l = 0.1;

                            -Larry Jones

                            I'll be a hulking, surly teen-ager before you know it!! -- Calvin

                            Comment

                            • Jordan Abel

                              #15
                              Re: rationale for #define true 1 in stdbool.h

                              On 2006-01-11, Ben Hinkle <ben.hinkle@gma il.com> wrote:[color=blue]
                              >
                              > "Chuck F. " <cbfalconer@yah oo.com> wrote in message
                              > news:9sednefape aDPFneRVn-hg@maineline.ne t...[color=green]
                              >> Eric Sosman wrote:[color=darkred]
                              >>>[/color]
                              >> ... snip ...[color=darkred]
                              >>>
                              >>> Thirty-five-plus years of C somehow scraped by without anything
                              >>> more, and I still don't see what _Bool brings to the party.
                              >>> Well, maybe I'm just a party pooper.[/color]
                              >>
                              >> You can always avoid it by simply failing to #include <stdbool.h>. Then
                              >> the only evidence remaining is an identifier in the implementors
                              >> namespace. But for people who do want to define true, false, and bool,
                              >> everything is standardized.[/color]
                              >
                              > But if the intention is that stdbool be used for standard boolean coding
                              > then it will appear in headers and once you include someone's header that
                              > uses it then you are using it, too. So if the intention of stdbool is that
                              > it is included in, say, 90% of headers that involve function taking a
                              > boolean parameter then as an individual there's not much one can do to avoid
                              > it. However if the intention is that it gets used by a few individuals (more
                              > particuarly, not in library headers) then how it behaves isn't a big deal.
                              >
                              > -Ben[/color]

                              function declarations in headers could just use _Bool instead.

                              Comment

                              Working...