Problem evaluating conditional operator

Discussion in 'C++' started by Richard Tierney, Jul 27, 2004.

  1. 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;
    }
     
    Richard Tierney, Jul 27, 2004
    #1
    1. Advertising

  2. Richard Tierney

    Rolf Magnus Guest

    Richard Tierney wrote:

    > 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.

    > else
    > cout << dec << z << endl;
    > }
     
    Rolf Magnus, Jul 27, 2004
    #2
    1. Advertising

  3. Richard Tierney

    marbac Guest

    Richard Tierney wrote:
    > 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;
    }
     
    marbac, Jul 27, 2004
    #3
  4. On Tue, 27 Jul 2004 11:07:53 +0200, Rolf Magnus <>
    wrote:

    <snipped>

    That's what I call service - an answer in 10 minutes!

    :)

    Richard
     
    Richard Tierney, Jul 27, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alec S.
    Replies:
    10
    Views:
    10,194
    Alec S.
    Apr 16, 2005
  2. Bill Davidson
    Replies:
    2
    Views:
    337
    Claudio Jolowicz
    Apr 21, 2004
  3. Mahesh Tomar

    Conditional operator problem

    Mahesh Tomar, Jul 21, 2003, in forum: C Programming
    Replies:
    6
    Views:
    370
    Eric Sosman
    Jul 22, 2003
  4. bnp
    Replies:
    30
    Views:
    848
    Ralmin
    May 25, 2004
  5. Replies:
    2
    Views:
    181
Loading...

Share This Page