449,277 Members | 1,161 Online 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
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

 P: n/a Jacek Dziedzic

 P: n/a Steve Pope I _think_ std::vector

### This discussion thread is closed

Replies have been disabled for this discussion. 