operator ?:

V

Victor Bazarov

Kuba_O said:
I have a piece of code:

int foo;
for(;;)
{
foo=someFunc(shmoo);
(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?

'break' is not an expression.

V
 
K

Kuba_O

Hello.
I have a piece of code:

int foo;
for(;;)
{
foo=someFunc(shmoo);
(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.
 
S

Steven T. Hatton

Victor said:
'break' is not an expression.

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

Stuart MacMartin

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
 
K

Kuba_O

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

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

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

Steven T. Hatton

Kuba_O said:
Thursday 21 of July 2005 17:57, Steven T. Hatton :


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

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

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:

http://82.229.136.165/localdoc/Cppdraft/expr.html
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.temporary_) 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.
 
V

Victor Bazarov

Kuba_O said:
Thursday 21 of July 2005 17:57, Steven T. Hatton :




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

In some cases it can. Generally speaking, it can't.
BTW is it _require_ to '?:' return value?

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

Dietmar Kuehl

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

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

Steven T. Hatton

Dietmar said:
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.

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.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top