By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,277 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,277 IT Pros & Developers. It's quick & easy.

The true nature of "bool"?

P: n/a

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
Share this Question
Share on Google+
4 Replies


P: n/a
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
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 24 '06 #2

P: n/a
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

P: n/a
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

P: n/a
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.