Problem evaluating conditional operator

R

Richard Tierney

I'd like to do something like this:

cout << (A? x : B? y:z) << endl;

where x, y, and z are signed and unsigned versions of the same type.
However, this doesn't work. When the expression in brackets is
evaluated, it does something more complicated than simply returning
one of x, y, or z. Can someone tell me what the line above actually
does? Complete (short) test program below.

Thanks -

Richard

----------------------------------------------------------
#include <iostream>
#include <iomanip>

using std::dec;
using std::cout;
using std::endl;

main() {
bool A = false;
bool B = true;
unsigned x = 10;
int y = -20;
int z = 30;
cout << (A? x : B? y:z) << endl; // this prints '4294967276'..
if(A)
cout << dec << x << endl;
else if(B)
cout << dec << y << endl; // but this prints '-20'!
else
cout << dec << z << endl;
}
 
R

Rolf Magnus

Richard said:
I'd like to do something like this:

cout << (A? x : B? y:z) << endl;

where x, y, and z are signed and unsigned versions of the same type.

C++ is a statically typed language. The return type of operator ?: is
fixed at compile time and cannot depend on the value of A.
However, this doesn't work. When the expression in brackets is
evaluated, it does something more complicated than simply returning
one of x, y, or z.

It also converts them all to the same type.
Can someone tell me what the line above actually does? Complete
(short) test program below.

Thanks -

Richard

----------------------------------------------------------
#include <iostream>
#include <iomanip>

using std::dec;
using std::cout;
using std::endl;

main() {
bool A = false;
bool B = true;
unsigned x = 10;
int y = -20;
int z = 30;
cout << (A? x : B? y:z) << endl; // this prints '4294967276'..

x is an unsigned int, therefore the return type of the outer operator ?:
becomes unsigned int. From the second one, y is chosen, which is then
converted to unsigned int.
if(A)
cout << dec << x << endl;
else if(B)
cout << dec << y << endl; // but this prints '-20'!

Of course. That's what the value of y is.
 
M

marbac

Richard said:
I'd like to do something like this:

cout << (A? x : B? y:z) << endl;

where x, y, and z are signed and unsigned versions of the same type.
However, this doesn't work. When the expression in brackets is
evaluated, it does something more complicated than simply returning
one of x, y, or z. Can someone tell me what the line above actually
does? Complete (short) test program below.

this should work correctly:

#include <iostream>
#include <iomanip>

using std::dec;
using std::cout;
using std::endl;

main() {
bool A = false;
bool B = true;
unsigned x = 10;
int y = -20;
int z = 30;

cout << (int)(A? x : B? y:z) << endl; // this prints '4294967276'..
if(A)
cout << dec << x << endl;
else if(B)
cout << dec << y << endl; // but this prints '-20'!
else
cout << dec << z << endl;
}
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top