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 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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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?")?
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |