operator ?:

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kuba_O

    operator ?:

    Hello.
    I have a piece of code:

    int foo;
    for(;;)
    {
    foo=someFunc(sh moo);
    (foo==10) ? break : cout<<foo<<endl ;
    }

    And compiler throw error:
    "ISO C++ forbids omitting the middle term of a ?: expression"

    So what did i omit here?

    I use g++ 3.3.5 with -Wall -pedantic.

    --
    empty
  • Victor Bazarov

    #2
    Re: operator ?:

    Kuba_O wrote:[color=blue]
    > I have a piece of code:
    >
    > int foo;
    > for(;;)
    > {
    > foo=someFunc(sh moo);
    > (foo==10) ? break : cout<<foo<<endl ;
    > }
    >
    > And compiler throw error:
    > "ISO C++ forbids omitting the middle term of a ?: expression"
    >
    > So what did i omit here?[/color]

    'break' is not an expression.

    V

    Comment

    • Steven T. Hatton

      #3
      Re: operator ?:

      Victor Bazarov wrote:
      [color=blue]
      > Kuba_O wrote:[color=green]
      >> I have a piece of code:
      >>
      >> int foo;
      >> for(;;)
      >> {
      >> foo=someFunc(sh moo);
      >> (foo==10) ? break : cout<<foo<<endl ;
      >> }
      >>
      >> And compiler throw error:
      >> "ISO C++ forbids omitting the middle term of a ?: expression"
      >>
      >> So what did i omit here?[/color]
      >
      > 'break' is not an expression.
      >
      > V[/color]
      This may be informal, but the way I always think about the ?: expression is
      to assume its result is being assigned to something. In that case I would
      ask myself if `some_var = break;' is meaningful.
      --
      If our hypothesis is about anything and not about some one or more
      particular things, then our deductions constitute mathematics. Thus
      mathematics may be defined as the subject in which we never know what we
      are talking about, nor whether what we are saying is true.-Bertrand Russell

      Comment

      • Stuart MacMartin

        #4
        Re: operator ?:

        Most compilers expect the two terms in the ternary operator to be the
        same type. Breaking out of a ternary operator seems to me to be
        pushing your luck and beyond the scope of what a ternary operator is
        for. (What does "break" return?) Just use the corresponding if
        statement since that's what you really want anyway.

        Most people use ternary operators sparingly, in (old code) macros or in
        reference initialization or in straightforward initialization of
        integers or flags.

        Stuart

        Comment

        • Kuba_O

          #5
          Re: operator ?:

          Thursday 21 of July 2005 17:57, Steven T. Hatton :
          [color=blue]
          > This may be informal, but the way I always think about the ?:
          > expression is
          > to assume its result is being assigned to something.[/color]

          Well, i was thinking it can be used instead of simple 'if'.

          BTW is it _require_ to '?:' return value?

          --
          empty

          Comment

          • Steven T. Hatton

            #6
            Re: operator ?:

            Kuba_O wrote:
            [color=blue]
            > Thursday 21 of July 2005 17:57, Steven T. Hatton :
            >[color=green]
            >> This may be informal, but the way I always think about the ?:
            >> expression is
            >> to assume its result is being assigned to something.[/color]
            >
            > Well, i was thinking it can be used instead of simple 'if'.
            >
            > BTW is it _require_ to '?:' return value?
            >[/color]

            The answer seems to be that you can have a conditional expression that does
            not return a value. I believe this is the same as in the current Standard:


            5.16 Conditional operator [expr.cond]

            1 conditional-expression:
            logical-or-expression
            logical-or-expression ? expression : assignment-expression
            Conditional expressions group right-to-left. The first expression is

            implicitly converted to bool (_conv_). It is evaluated and if it is
            true, the result of the conditional expression is the value of the
            second expression, otherwise that of the third expression. All side
            effects of the first expression except for destruction of temporaries
            (_class.tempora ry_) happen before the second or third expression is
            evaluated. Only one of the second and third expressions is evaluated.

            2 If either the second or the third operand has type (possibly cv-quali-
            fied) void, then the lvalue-to-rvalue (_conv.lval_), array-to-pointer
            (_conv.array_), and function-to-pointer (_conv.func_) standard conver-
            sions are performed on the second and third operands, and one of the
            following shall hold:

            - -The second or the third operand (but not both) is a throw-expression
            (_except.throw_ ); the result is of the type of the other and is an
            rvalue.

            - -Both the second and the third operands have type void; the result is
            of type void and is an rvalue. [Note: this includes the case where
            both operands are throw-expressions. ]

            I would say using such a construct as `test?throw this_exception; throw
            that_exception; is illadvised as a means of flow control outside of
            exception handling. IOW, it should not be used to handle frequently
            occurring conditions.
            --
            If our hypothesis is about anything and not about some one or more
            particular things, then our deductions constitute mathematics. Thus
            mathematics may be defined as the subject in which we never know what we
            are talking about, nor whether what we are saying is true.-Bertrand Russell

            Comment

            • Victor Bazarov

              #7
              Re: operator ?:

              Kuba_O wrote:[color=blue]
              > Thursday 21 of July 2005 17:57, Steven T. Hatton :
              >
              >[color=green]
              >>This may be informal, but the way I always think about the ?:
              >>expression is
              >>to assume its result is being assigned to something.[/color]
              >
              >
              > Well, i was thinking it can be used instead of simple 'if'.[/color]

              In some cases it can. Generally speaking, it can't.
              [color=blue]
              > BTW is it _require_ to '?:' return value?[/color]

              Essentially, yes, both parts surrounding the ':' have to be _expressions_,
              not statements.

              Comment

              • Dietmar Kuehl

                #8
                Re: operator ?:

                Victor Bazarov wrote:[color=blue][color=green]
                >> BTW is it _require_ to '?:' return value?[/color]
                >
                > Essentially, yes, both parts surrounding the ':' have to be _expressions_,
                > not statements.[/color]

                There is no need to return a value, although both parts have to
                be expressions: both expressions can, for example, be 'void'
                (essentially, this is only possible by calling functions returning
                'void'). Also, one of the parts can throw an exceptions. I don't
                think both parts can be throw expressions.
                --
                <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
                <http://www.eai-systems.com> - Efficient Artificial Intelligence

                Comment

                • Steven T. Hatton

                  #9
                  Re: operator ?:

                  Dietmar Kuehl wrote:
                  [color=blue]
                  > Victor Bazarov wrote:[color=green][color=darkred]
                  >>> BTW is it _require_ to '?:' return value?[/color]
                  >>
                  >> Essentially, yes, both parts surrounding the ':' have to be
                  >> _expressions_, not statements.[/color]
                  >
                  > There is no need to return a value, although both parts have to
                  > be expressions: both expressions can, for example, be 'void'
                  > (essentially, this is only possible by calling functions returning
                  > 'void'). Also, one of the parts can throw an exceptions. I don't
                  > think both parts can be throw expressions.[/color]

                  The wording is a bit tricky, but as I read ยง15.6 both the second and third
                  operand can be throw expressions.

                  "Both the second and the third operands have type void; the result is of
                  type void and is an rvalue.
                  [Note: this includes the case where both operands are throw-expressions. ]"

                  What I'm not sure of is the meaning of:

                  "If either the second or the third operand has type (possibly cv-qualified)
                  void, then the lvalue-to-rvalue (4.1), array-to-pointer (4.2), and
                  function-to-pointer (4.3) standard conversions are performed on the second
                  and third operands,"

                  But I haven't looked into it either. What I'm not sure of is whether this
                  means the function could retrun a void pointer or something like that.

                  --
                  If our hypothesis is about anything and not about some one or more
                  particular things, then our deductions constitute mathematics. Thus
                  mathematics may be defined as the subject in which we never know what we
                  are talking about, nor whether what we are saying is true.-Bertrand Russell

                  Comment

                  Working...