Does bool true always == 1?

N

Nollie

Does standard c++ define "true" to be always equal to 1?

My code:
int num = 1 + (value1== value2);

Can I be certain that "num" will ALWAYS be either 1 or 2? How about
on a Windows machine?
 
K

Karl Heinz Buchegger

Nollie said:
Does standard c++ define "true" to be always equal to 1?

Yes.
When a bool value is converted to an integer value, then
false -> 0
true -> 1
My code:
int num = 1 + (value1== value2);

Can I be certain that "num" will ALWAYS be either 1 or 2? How about
on a Windows machine?

This is not a question of Windows or not Windows.
This is a question of "C++ - The language".
 
C

Chris Theis

Nollie said:
Does standard c++ define "true" to be always equal to 1?

My code:
int num = 1 + (value1== value2);

Can I be certain that "num" will ALWAYS be either 1 or 2? How about
on a Windows machine?

Quoting from the standard about integral prompotions (4.5)

"An rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one."

Cheers

Chris
 
A

Alf P. Steinbach

* Nollie:
Does standard c++ define "true" to be always equal to 1?

In the sense of always converting to 1, yes.

My code:
int num = 1 + (value1== value2);

Can I be certain that "num" will ALWAYS be either 1 or 2?

Yes for built-in types.

No in general, because it's possible for the programmer to define the meaning
of '=='.

How about on a Windows machine?

Irrelevant.
 
R

Richard Cavell

"An rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one."

int value1 = 2000 ;
int value2 = 2000 ;
int num = 1 + (value1== value2);
std::cout << num << "\n";

num = 1 + reinterpret_cast<int> (value1 == value2);
std::cout << num << "\n";

The first num is 2. The reinterpret_cast doesn't compile, stating that
bool cannot be converted to int.
 
A

Andre Kostur

int value1 = 2000 ;
int value2 = 2000 ;
int num = 1 + (value1== value2);
std::cout << num << "\n";

num = 1 + reinterpret_cast<int> (value1 == value2);
std::cout << num << "\n";

The first num is 2. The reinterpret_cast doesn't compile, stating that
bool cannot be converted to int.

Using the wrong cast... try static_cast<int>
 
C

Chris Theis

Richard Cavell said:
int value1 = 2000 ;
int value2 = 2000 ;
int num = 1 + (value1== value2);
std::cout << num << "\n";

num = 1 + reinterpret_cast<int> (value1 == value2);
std::cout << num << "\n";

The first num is 2. The reinterpret_cast doesn't compile, stating that
bool cannot be converted to int.

You're trying to shoot a dove with a cannon here. What you're looking for is
static_cast and not reinterpret_cast, which is something you should consider
your last resort only.

Chris
 
V

Victor Bazarov

Chris said:
You're trying to shoot a dove with a cannon here. What you're looking for is
static_cast and not reinterpret_cast, which is something you should consider
your last resort only.

I think the point here should be that 'reinterpret_cast' cannot be used to
convert 'bool' into 'int'. There are several conversion that can be made
using 'reinterpret_cast' and 'bool->int' simply isn't one of them. I mean
while shooting a dove with a cannot is in general possible, the Standard
expressly *prohibits* using 'reinterpret_cast' for any conversion *except*
the ones it lists.

V
 
R

Richard Cavell

You're trying to shoot a dove with a cannon here. What you're looking for is
static_cast and not reinterpret_cast, which is something you should consider
your last resort only.

Yeah, well I realize that, but I'm trying to demonstrate that bool does
not appear to be internally represented as (int) 1.
 
V

Victor Bazarov

Richard said:
Yeah, well I realize that, but I'm trying to demonstrate that bool
does not appear to be internally represented as (int) 1.

Why should anybody care about that? The internal representation of the
'bool' type is an implementation detail. It is not specified by the
language nor does it have to be the same on all implementations.

V
 
C

Chris Theis

Richard Cavell said:
Yeah, well I realize that, but I'm trying to demonstrate that bool does
not appear to be internally represented as (int) 1.

Well, the internal representation is actually of no concern to the user as
long as there is a conversion from bool to int, which the standard mandates.

Cheers
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top