Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > rationale for #define true 1 in stdbool.h

Reply
Thread Tools

rationale for #define true 1 in stdbool.h

 
 
Keith Thompson
Guest
Posts: n/a
 
      01-11-2006
"Alex Fraser" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> [snip]
>> Making true and false be of type _Bool wouldn't be very useful, since
>> they'd be promoted to int in most contexts anyway.

>
> Which leads to the obvious question: why are "small" types promoted to int?


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_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
 
Ben Hinkle
Guest
Posts: n/a
 
      01-11-2006

"Chuck F. " <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Eric Sosman wrote:
>>

> ... snip ...
>>
>> 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.

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


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


 
Reply With Quote
 
 
 
 
lawrence.jones@ugs.com
Guest
Posts: n/a
 
      01-11-2006
Eric Sosman <(E-Mail Removed)> wrote:
>
> Personally, I still don't understand the motivation for
> adding _Bool to the language.


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
 
Reply With Quote
 
lawrence.jones@ugs.com
Guest
Posts: n/a
 
      01-11-2006
Old Wolf <(E-Mail Removed)> wrote:
>
> 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 [...]


unsigned long long l = 0.1;

-Larry Jones

I'll be a hulking, surly teen-ager before you know it!! -- Calvin
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      01-11-2006
On 2006-01-11, Ben Hinkle <(E-Mail Removed)> wrote:
>
> "Chuck F. " <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Eric Sosman wrote:
>>>

>> ... snip ...
>>>
>>> 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.

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

>
> 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


function declarations in headers could just use _Bool instead.
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      01-12-2006
Eric Sosman wrote:
> Old Wolf wrote:
>> Eric Sosman wrote:
>>> Personally, I still don't understand the motivation for
>>>adding _Bool to the language.

>>
>> I have accidentally written code like this:
>>
>> bool b = (flags & FLAG_FOO);
>>
>> where FLAG_FOO is something like 0x100. I

>
> 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 fact my situation was a bit more disguised:

bool test_foo(FLAGS flags)
{
return flags & FLAG_FOO;
}

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


Yes, once I had been alerted to the danger, I went through all of
my source-code looking for such things, and did that to them.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
Question about rationale for java.nio.Buffer design. Harald Kirsch Java 0 06-14-2004 12:15 PM
Re: The Sigma-Foveon pixel rationale Dave Martindale Digital Photography 2 04-02-2004 08:49 PM
Re: The Sigma-Foveon pixel rationale Dave Martindale Digital Photography 2 04-01-2004 05:01 PM
Re: The Sigma-Foveon pixel rationale David J. Littleboy Digital Photography 2 04-01-2004 08:11 AM



Advertisments