anything whcih can be done with ternary but not with if else

Discussion in 'C++' started by gupta.keshav@gmail.com, Oct 17, 2005.

  1. Guest

    HI,
    Is there any situation which can be handled by ternary operator but
    not with if else blocks?
    Thanks
    Keshav
    , Oct 17, 2005
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > HI,
    > Is there any situation which can be handled by ternary operator but
    > not with if else blocks?


    class Foo
    {
    public:
    Foo(bool b)
    : x_(b ? 42 : 7)
    {
    }
    private:
    const int x_;
    };
    Rolf Magnus, Oct 17, 2005
    #2
    1. Advertising

  3. mlimber Guest

    Rolf Magnus wrote:
    > wrote:
    >
    > > HI,
    > > Is there any situation which can be handled by ternary operator but
    > > not with if else blocks?

    >
    > class Foo
    > {
    > public:
    > Foo(bool b)
    > : x_(b ? 42 : 7)
    > {
    > }
    > private:
    > const int x_;
    > };


    And conditionally assigning constants in general. Compare:

    void Foo( const bool b )
    {
    const int i = b ? 1 : 2;
    // ...
    }

    void Bar( const bool b )
    {
    // i cannot be const
    int i = 0;
    if( b )
    {
    i = 1;
    }
    else
    {
    i = 2;
    }
    // ...
    }

    Cheers! --M
    mlimber, Oct 17, 2005
    #3
  4. John Carson Guest

    <> wrote in message
    news:
    > HI,
    > Is there any situation which can be handled by ternary operator but
    > not with if else blocks?
    > Thanks
    > Keshav


    bool b;
    int x, y;

    // stuff setting b, x and y

    int & ref = b ? x, y;

    Note that you can do

    if(b)
    int & ref = x;
    else
    int & ref = y;

    but the scope of the reference is limited to the body of the if-else
    conditions. You can't get around this by declaring the reference before the
    if statement because a reference must be initialised at its point of
    declaration.

    --
    John Carson
    John Carson, Oct 17, 2005
    #4
  5. shailendra Guest

    what if I'm doing this?

    int const * foo( const bool b )
    {
    static int i = 0;

    if( b )
    {
    i = 1;
    }
    else
    {
    i = 2;
    }
    return &i;
    }
    shailendra, Oct 17, 2005
    #5
  6. Fraser Ross Guest

    int const * foo( const bool b )
    {
    static int i;
    return &(i=b ? 1 : 2);
    }


    This is the equivalent using the ternary operator making the function as
    short as possible. This doesn't give an example that the OP is looking
    for.

    Fraser.
    Fraser Ross, Oct 17, 2005
    #6
  7. mlimber Guest

    shailendra wrote:
    > what if I'm doing this?
    >
    > int const * foo( const bool b )
    > {
    > static int i = 0;
    >
    > if( b )
    > {
    > i = 1;
    > }
    > else
    > {
    > i = 2;
    > }
    > return &i;
    > }


    It should work fine, and you could use the ternary operator, though not
    on the initialization of the static:

    int const* foo( const bool b )
    {
    static int i = 0; // Only executed once
    i = b ? 1 : 2; // Executed each time foo is called
    return &i;
    }

    Cheers! --M
    mlimber, Oct 17, 2005
    #7
  8. shailendra Guest

    My reply was to the examples given by "mlimber" that, there is a way to
    substitute his/her idea --

    So given example is wrong, coz this isn't the only situatuation where
    ternery can be applied. So we better find some good situation only for
    ternery.

    Frankly speaking, I dont think, there is any!!
    shailendra, Oct 17, 2005
    #8
  9. Jay Nabonne Guest

    On Mon, 17 Oct 2005 05:49:18 -0700, gupta.keshav wrote:

    > HI,
    > Is there any situation which can be handled by ternary operator but
    > not with if else blocks?


    I'm not sure there is such a case *if* you're allowed to put the if/else
    in a separately called function.

    e.g.

    class Foo
    {
    public:
    Foo(bool b)
    : x_(getit(b))
    {
    }

    int getit(bool b)
    {
    if (b)
    return 42;
    else
    return 7;
    }
    private:
    const int x_;
    };

    - Jay
    Jay Nabonne, Oct 17, 2005
    #9
  10. Mike Smith Guest

    shailendra wrote:
    > My reply was to the examples given by "mlimber" that, there is a way to
    > substitute his/her idea --
    >
    > So given example is wrong, coz this isn't the only situatuation where
    > ternery can be applied. So we better find some good situation only for
    > ternery.
    >
    > Frankly speaking, I dont think, there is any!!


    One has already been given.

    --
    Mike Smith
    Mike Smith, Oct 17, 2005
    #10
  11. <> wrote in message
    news:...

    > Is there any situation which can be handled by ternary operator but
    > not with if else blocks?


    if-else requires assignability. Anything that doesn't support assignment
    cannot be used with if-else. In addition to the already provided constructor
    initialization list, three more that I can think of:


    1) A class that doesn't define operator=:

    Class UnAssignable{ /* ... */ };

    /* copy construction */
    UnAssignable object = condition ? first : second;


    2) Initializing a reference:

    Class & reference = condition ? first : second;


    3) Initializing a constant:

    SomeType const object = condition ? first : second;

    Ali
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Oct 17, 2005
    #11
  12. persenaama Guest

    You aren't replacing the ternary operator with if-else block but with a
    function call with if-else block inside it. There's a subtle
    difference, I think. A good suggestion, just happened to think, "wait
    just a minute...!"
    persenaama, Oct 17, 2005
    #12
  13. Old Wolf Guest

    mlimber wrote:
    > Rolf Magnus wrote:
    >> wrote:
    >>
    >>> Is there any situation which can be handled by ternary operator
    >>> but not with if else blocks?

    >>
    >> class Foo
    >> {
    >> public:
    >> Foo(bool b) : x_(b ? 42 : 7) { }
    > > private:
    > > const int x_;
    > > };

    >
    > And conditionally assigning constants in general. Compare:
    >
    > void Foo( const bool b )
    > {
    > const int i = b ? 1 : 2;
    > // ...
    > }


    In this case it can be done with if...else:
    void Foo( const bool b )
    {
    int i_;
    if (b)
    i_ = 1;
    else
    i_ = 2;
    const int i = i_;
    }
    Old Wolf, Oct 17, 2005
    #13
  14. Old Wolf Guest

    Rolf Magnus wrote:
    > wrote:
    >
    >> Is there any situation which can be handled by ternary operator but
    >> not with if else blocks?

    >
    > class Foo
    > {
    > public:
    > Foo(bool b)
    > : x_(b ? 42 : 7)
    > {
    > }
    > private:
    > const int x_;
    > };


    Note that this doesn't imply that ?: adds any new functionality
    to the language; (b ? 42 : 7) is equivalent to (b * 35 + 7).

    However I can't think of a replacment for (b ? foo() : bar()) offhand.
    Old Wolf, Oct 17, 2005
    #14
  15. Guest

    wht can be done is

    bool b;
    int x,y,z;
    if (b) z= x; else z = y;
    int &i = z;
    , Oct 18, 2005
    #15
  16. Greg Comeau Guest

    What can also be done with ternary is it can be put
    into a comma operator list, perhaps as part of a macro
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Oct 18, 2005
    #16
  17. Rolf Magnus Guest

    Greg Comeau wrote:

    > What can also be done with ternary is it can be put
    > into a comma operator list, perhaps as part of a macro


    And it can be used in template arguments.
    Rolf Magnus, Oct 18, 2005
    #17
  18. John Carson Guest

    <> wrote in message
    news:
    > wht can be done is
    >
    > bool b;
    > int x,y,z;
    > if (b) z= x; else z = y;
    > int &i = z;


    That is not equivalent to

    int &i = b ? x, y;

    Suppose that the code is followed by:

    i = 6;

    With my code, either x or y will become 6. With your code, x and y are not
    affected; the change is to z.


    --
    John Carson
    John Carson, Oct 18, 2005
    #18
  19. ben Guest

    Old Wolf wrote:
    > Rolf Magnus wrote:
    >
    >> wrote:
    >>
    >>
    >>>Is there any situation which can be handled by ternary operator but
    >>>not with if else blocks?

    >>
    >>class Foo
    >>{
    >>public:
    >> Foo(bool b)
    >> : x_(b ? 42 : 7)
    >> {
    >> }
    >>private:
    >> const int x_;
    >>};

    >
    >
    > Note that this doesn't imply that ?: adds any new functionality
    > to the language; (b ? 42 : 7) is equivalent to (b * 35 + 7).
    >
    > However I can't think of a replacment for (b ? foo() : bar()) offhand.
    >


    int f(bool b)
    {
    if (b) return foo();
    else return bar();
    }

    class Foo
    {
    public;
    Foo(bool b)
    : x_(f(b))

    .....

    ben
    ben, Oct 18, 2005
    #19
  20. Jay Nabonne Guest

    On Mon, 17 Oct 2005 13:26:31 -0700, persenaama wrote:

    > You aren't replacing the ternary operator with if-else block but with a
    > function call with if-else block inside it. There's a subtle
    > difference, I think. A good suggestion, just happened to think, "wait
    > just a minute...!"


    You're quite right. I mostly mentioned it because, believe it or not, I
    have seen coding guidelines that forbid the use of the ternary operator!
    So knowing how to live without it can be useful (though I wouldn't want to).

    - Jay
    Jay Nabonne, Oct 18, 2005
    #20
    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. =?Utf-8?B?YW4gdXNlcg==?=

    can't download anything else from internet

    =?Utf-8?B?YW4gdXNlcg==?=, Apr 30, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,932
    Ramu Nallamothu
    May 4, 2006
  2. Tor
    Replies:
    3
    Views:
    324
    Erik Max Francis
    Jul 8, 2003
  3. Bob Gailer
    Replies:
    16
    Views:
    563
    Mark Jackson
    Jul 8, 2003
  4. John Hunter
    Replies:
    1
    Views:
    318
    Grant Edwards
    Jul 7, 2003
  5. mast4as
    Replies:
    11
    Views:
    437
    Keith H Duggar
    Apr 20, 2010
Loading...

Share This Page