468,457 Members | 1,508 Online

# The true nature of "bool"?

Does "bool" behave _exactly_ like any other integer type, except for when it
comes to integer promotion? Consider the following C function whose purpose
it is to calculate the quantity of equal coordinates:

struct Coord3D {
int a,b,c;
};

typedef struct Coord3D Coord3D; /* To make it work like C++ */

unsigned QuantSame(Coord3D const *const x,Coord3D const *const y)
{
return (x->a == y->a)+(x->b == y->b)+(x->c == y->c);
}

In C, the type of the result of the equality operation would be "int", and
these int's can be added together.

In C++, however, the type of the result of the equality operation is "bool".
Am I right in thinking, that, when arithmetical operations are performed upon
a bool, that it undergoes integer promotion just like any other integer type?
(except that it _must_ become either 0 or 1).

Are there any other places in which a bool doesn't behave like it's just
another built-in integer type?

--

Frederick Gotham
Sep 24 '06 #1
4 1816
Frederick Gotham wrote:
Does "bool" behave _exactly_ like any other integer type, except for when it
comes to integer promotion? Consider the following C function whose purpose
it is to calculate the quantity of equal coordinates:

struct Coord3D {
int a,b,c;
};

typedef struct Coord3D Coord3D; /* To make it work like C++ */

unsigned QuantSame(Coord3D const *const x,Coord3D const *const y)
{
return (x->a == y->a)+(x->b == y->b)+(x->c == y->c);
}

In C, the type of the result of the equality operation would be "int", and
these int's can be added together.

In C++, however, the type of the result of the equality operation is "bool".
Am I right in thinking, that, when arithmetical operations are performed upon
a bool, that it undergoes integer promotion just like any other integer type?
(except that it _must_ become either 0 or 1).
Operands of type bool are promoted to int when used in arithmetic
expressions. false becomes 0 and true becomes 1.
Are there any other places in which a bool doesn't behave like it's just
another built-in integer type?
++ and -- are idiosyncratic.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
www.petebecker.com/tr1book.
Sep 24 '06 #2
Frederick Gotham wrote:
Does "bool" behave _exactly_ like any other integer type, except for when it
comes to integer promotion? Consider the following C function whose purpose
it is to calculate the quantity of equal coordinates:
I _think_ std::vector<boolbehaves in a weird manner.

HTH,
- J.
Sep 25 '06 #3
Jacek Dziedzic <jacek@no_spam.tygrys.no_spam.netwrote:
I _think_ std::vector<boolbehaves in a weird manner.
Yes, it is bit-packed. (Not sure if this is specified or just
the common implementation.) You almost never want to use it.

The comment about ++ and -- operators not behaving as for integer
types is also correct.

Steve
Sep 25 '06 #4
Steve Pope <sp*****@speedymail.orgwrote:
Jacek Dziedzic <jacek@no_spam.tygrys.no_spam.netwrote:
> I _think_ std::vector<boolbehaves in a weird manner.

Yes, it is bit-packed. (Not sure if this is specified or just
the common implementation.) You almost never want to use it.
Also, many of the operations in std::vector<boolreturn a proxy object
instead of a reference to bool; see:

http://www.gotw.ca/publications/mill09.htm
http://www.gotw.ca/publications/N1185.pdf
http://www.gotw.ca/publications/N1211.pdf

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 25 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.