473,394 Members | 1,893 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,394 software developers and data experts.

int or bool?

Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?

Thanks
Fred
Jul 22 '05 #1
12 3267
Fred wrote:
Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?


First of all, I like bool because it lets me say what I mean. If I want
a function to return a "true" or "false" value, I make it return bool,
because that is exactly what I mean.

They are totally interchangable, because 1!=-1, but bool(1)==bool(-1).
Bools can in many ways be considered "almost the same as" ints where you
are only allowed 0 or 1, but they impose this condition for you rather
than forcing you to make sure you apply it yourself.

You can of course write functions and templates which behave differently
depending on which one they are given, but of course this is true for
all types.

Chris
Jul 22 '05 #2
"Fred" <Fr**@somewhere.abc> wrote in message
news:cp**********@titan.btinternet.com...
Why was the bool type introduced into c++? See for example:
http://www.gotw.ca/gotw/026.htm
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?

bool only has two possible values/states: true and false.

Expressions such as a != b have a different meaning
for bool than for int.
When a and b are of bool type, it is equivalent to a logical x-or.

One should use bool whenever a value can only be true or false,
just as one should use int rather than double as it helps avoid
bugs.

hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #3
"Fred" <Fr**@somewhere.abc> wrote in message
news:cp**********@titan.btinternet.com...
Why was the bool type introduced into c++?
Because the practice of defining one's own synonym for int and using it as a
boolean type had grown to the point where large systems typically had
multiple incompatible definitions of boolean types.
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?


They are not interchangeable. For one thing, if int and bool are different
types, you can overload on them.
Jul 22 '05 #4
Fred wrote:
Why was the bool type introduced into c++?
What does it provide that int does not and are the two
entirely interchangable in conditional expressions?


It was introduced because lots of code was creating its own bool types
and they were incompatible with each other. When done with macros, this
creates problems if two libraries try to define TRUE differently. With
enums in the global scope, it will fail to compile. Also, not everyone
made their "true" convertable to 1 like the standard does. Someone
might define "true" as -1, and then two different concepts of "true"
would compare unequal.

--
Dave O'Hearn

Jul 22 '05 #5

"Fred" <Fr**@somewhere.abc> wrote in message
news:cp**********@titan.btinternet.com...
Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?


Using a number to represent a boolean was a hack to get around the fact that
there was no boolean. Kind of like using char* because there was no String
class.
Jul 22 '05 #6

"Andrew Koenig" <ar*@acm.org> wrote in message
news:zd**********************@bgtnsc04-news.ops.worldnet.att.net...
"Fred" <Fr**@somewhere.abc> wrote in message
news:cp**********@titan.btinternet.com...
Why was the bool type introduced into c++?


Because the practice of defining one's own synonym for int and using it as a
boolean type had grown to the point where large systems typically had
multiple incompatible definitions of boolean types.


Not to mention the fact that it required work to define and make available such
a basic type to begin with.
Jul 22 '05 #7
One should use bool whenever a value can only be true or false,
ok
just as one should use int rather than double as it helps avoid
bugs.


what? I'd think one should use int when integers are required, and double
when floating-point numbers are required (and when the float type is not
otherwise required for whatever reason). What does this have to do with
avoiding bugs?

-Howard
Jul 22 '05 #8
Howard wrote:
One should use bool whenever a value can only be true or false,

ok

just as one should use int rather than double as it helps avoid
bugs.

what? I'd think one should use int when integers are required, and double
when floating-point numbers are required (and when the float type is not
otherwise required for whatever reason). What does this have to do with
avoiding bugs?

-Howard

Saying you can use an int to store a boolean value is like saying you
can use a double to store an int : you can do it but you can get a bug
for example if you store multiple values of the "bigger" set to
represent a single value of the "smaller" set.

bool a = true, b = true;
if( a && b )
assert( a == b ); // true

int a = -1, b = 1;
if( a && b )
assert( a == b ); // false
Jul 22 '05 #9

"Chris Jefferson" <ca*@cs.york.ac.uk> wrote in message
news:41**************@cs.york.ac.uk...
Fred wrote:
Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?


First of all, I like bool because it lets me say what I mean. If I want
a function to return a "true" or "false" value, I make it return bool,
because that is exactly what I mean.

They are totally interchangable, because 1!=-1, but bool(1)==bool(-1).


TCPPPL says nonzero integers convert to true and 0 converts to false.
Why all nonzero integers including negative integers should convert to zero?
Converting negative integers to false is as natural as converting 0 to
false?


Jul 22 '05 #10

"Vinodh Kumar P" <pv**********@gmail.com> wrote in message
news:cp**********@news.mch.sbs.de...

"Chris Jefferson" <ca*@cs.york.ac.uk> wrote in message
news:41**************@cs.york.ac.uk...
Fred wrote:
> Why was the bool type introduced into c++?
> What does it provide that int does not and are the two entirely
> interchangable in conditional expressions?
>


First of all, I like bool because it lets me say what I mean. If I want
a function to return a "true" or "false" value, I make it return bool,
because that is exactly what I mean.

They are totally interchangable, because 1!=-1, but bool(1)==bool(-1).


TCPPPL says nonzero integers convert to true and 0 converts to false.
Why all nonzero integers including negative integers should convert to
zero?
Converting negative integers to false is as natural as converting 0 to
false?


In what way is a negative number false?

The main problem with doing something like that, as I see it, would be the
"accidental" use of a signed integer instead of an unsigned integer, and
then your "true" value of, say, 32768 would become a "false" value, with no
change to the actual value.

Is the following true or false?

if (128) {...

If the constant 128 is put into a signed 8-bit char, it's a negative number.
But put it in an unsigned char, or into a larger integer variable, and now
it's a positive number! See the problem? Suddenly context becomes
important, but whoever is reporting 128 (such as returning it from a
function) meant only true or false, not both!

The idea of zero being false is much more natural, also, since it is often
used when evaluating pointers (esp. pointers returned from functions). For
example, one often sees something like:

if (GetSomePointer()) {...

A poiinter is essentially an unsigned integer value, and when that value is
zero, it means the null pointer, which points to nothing, and is the only
valid condition that can be checked for to determine if you have a valid
pointer or not.

This idea has been extended to the function that returns an error code,
where 0 means it's ok, and anything else indicates a possible error
condition, so that you will see:

if (DoSomethingAndReportResults()) {...

This is much more undestandable than doing something like this:

if (DoSomethingAndReportResults() > 0) {...,

which is what would be *implied* if the integer value returned could be
interpreted as false when it was negative. Hiding that implied condition
would be a bad thing, in my opinion, and lead to many errors. It's much
easier to just know that if and only if a value is zero, testing it as a
boolean condition would return false.

That's my 2 cents worth, anyway.

-Howard


Jul 22 '05 #11
Vinodh Kumar P wrote:
"Chris Jefferson" <ca*@cs.york.ac.uk> wrote in message
news:41**************@cs.york.ac.uk...
Fred wrote:
Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?


First of all, I like bool because it lets me say what I mean. If I want
a function to return a "true" or "false" value, I make it return bool,
because that is exactly what I mean.

They are totally interchangable, because 1!=-1, but bool(1)==bool(-1).

TCPPPL says nonzero integers convert to true and 0 converts to false.
Why all nonzero integers including negative integers should convert to zero?
Converting negative integers to false is as natural as converting 0 to
false?

One thing to consider is that in some old systems, -1 was the "default"
way of representing true, because in 2s-complient (the most common way
of representing signed numbers in computers), -1 is represented by all
1's in binary. Therefore there is a good argument for -1 (at least)
being true.

Chris
Jul 22 '05 #12

"Vinodh Kumar P" <pv**********@gmail.com> wrote in message
news:cp**********@news.mch.sbs.de...

"Chris Jefferson" <ca*@cs.york.ac.uk> wrote in message
news:41**************@cs.york.ac.uk...
Fred wrote:
Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?

First of all, I like bool because it lets me say what I mean. If I want
a function to return a "true" or "false" value, I make it return bool,
because that is exactly what I mean.

They are totally interchangable, because 1!=-1, but bool(1)==bool(-1).


They're interchangeable in a boolean context. But imo
type 'bool' and the keywords 'true' and 'false' more
clearly express intent.

TCPPPL says nonzero integers convert to true and 0 converts to false.
Correct.
Why all nonzero integers including negative integers should convert to zero?

Because that's the way it is. "Zero" meaning "off", "clear", "false",
any other value meaning "on", "set", "true".

Boolean true/false means exactly two possible states. If zero
is 'false', any other value cannot mean 'false', because any
other number is not zero.
Converting negative integers to false is as natural as converting 0 to
false?


I don't think so.

True. False.
Zero. Not zero.

Simple.

-Mike
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
4
by: Nomak | last post by:
Hello, With this code: $ cat -n ifs.cc 1 #include <vector> 2 #include <iostream> 3 4 using std::vector; 5 using std::cin;
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
4
by: ORC | last post by:
Is the bool type actually an Int32 with 0 as false and non zero as true? The reason for my question is that I've seen a lot of API calls that return a Int32 implemented in C# as an bool like: ...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
1
by: Bern McCarty | last post by:
What do you make of this? I cannot tell for sure but it almost seems as the the transition thunk to get back from the native bool method to the managed caller is looking at eax and, if any bit is...
4
by: gpg | last post by:
I am using a legacy DLL and need to marshal some structures for use in the DLL. For the most part, I have figured out my needs except for one small item. I have a structure that contain, among...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
64
by: shaanxxx | last post by:
I have code which says #define MYBOOL int This code is very old. people who have written is not avaible. I was thinking what could be reason. 1) bool datatype was not available that time (10...
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.