The true nature of "bool"?

F

Frederick Gotham

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

Pete Becker

Frederick said:
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
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
J

Jacek Dziedzic

Frederick said:
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<bool> behaves in a weird manner.

HTH,
- J.
 
S

Steve Pope

Jacek Dziedzic said:
I _think_ std::vector<bool> behaves 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top