473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2020
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
In the following loop, at the end, the method debugNotes() is printing out some notes for me to read, so I can figure out what is going on in my script. Where I try to print out the value of the...
3
by: Mochuelo | last post by:
Hi, In the constructor of a class, I am taking as a parameter the reference to a "bool", as in this code: ----------------------- public class TCP_synch_client { public string address...
2
by: Chris Wood | last post by:
In C#, I am calling a method implemented in Managed C++ that returns an array of booleans. This method in turn calls unto unmanaged C++ code that returns an unsigned byte array, which is...
59
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
2
by: Phil Davidson | last post by:
/* (Posted to microsoft.public.dotnet.languages.vc) In C++/CLI under Visual Studio 2005, creating an "enum class" member called "Equals" can cause compiler error C1060 (out of memory) and C1063...
2
by: abelniel | last post by:
Hi all, When I use "bool" type in my program, gcc pop an error error: `bool' undeclared (first use in this function) I have included the following lib: #include <stdio.h> #include...
3
by: knguyen | last post by:
Hello, I was trying to compile the source code from "Design Patterns: Elements of Reusable Object-Oriented Software", which can be obtained from here...
2
by: Marcel Kloubert | last post by:
Hi! Example: public bool? IsChecked { get { throw new NotImplementedException(); } } What does the question mark after the bool mean ("bool?")?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.