473,804 Members | 3,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3296
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.btinte rnet.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.btinte rnet.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.btinte rnet.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.worldn et.att.net...
"Fred" <Fr**@somewhere .abc> wrote in message
news:cp******** **@titan.btinte rnet.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

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

Similar topics

3
2613
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 similar behavior. My class looks like this: #ifndef long_int_H #define long_int_H #include <string> using namespace std; typedef valarray<complex<double> > VCD;
4
2953
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
3365
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 something else? I ask because I want to use vector<bool> with a few billion entries exceeding the int32 range for indexing and I want to use as few memory as possible for the whole
4
10756
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: private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags); Thanks Ole
10
19159
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 ray, int count)" Now I'm having problems with C++ "return(false)" being True in C#. Here is the C# code. ========================= using System; using System.Runtime.InteropServices; //
1
1604
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 set, normalizing it to 0x00000001. If it wants to normalize the value then it should only operate on the al register since that's all that the native bool method uses to hold the return value. Is this a known VC 7.1 bug? Is there a hotfix...
4
6479
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 other items, an array of bools (not BOOL). The array is a fixed size and is contained in the structure. Should be simple ie:
6
6831
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 vector<bool>, does each element takes 4 bytes instead of 1 bit? I am using gcc3.4.4. There is a bit_vector which is kind of old so I wont use that. Any other choices? Thanks ahead. zl2k
64
3904
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 years back: correct me if i am wrong) 2) int was word aligned (16bit and 32bit). Writter of above code doesnt want to give choice to compiler.
3
6327
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 that no matter if we pass TRUE or FALSE, the bool is always marshalled as true. // unmanaged code in dll typedef bool (__stdcall *BoolCallBack)(short b);
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.