By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

operator ?:

P: n/a
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.

--
empty
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Kuba_O wrote:
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
Jul 23 '05 #2

P: n/a
Victor Bazarov wrote:
Kuba_O wrote:
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

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
Jul 23 '05 #3

P: n/a
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

Jul 23 '05 #4

P: n/a
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?

--
empty
Jul 23 '05 #5

P: n/a
Kuba_O wrote:
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?


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.
--
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
Jul 23 '05 #6

P: n/a
Kuba_O wrote:
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'.


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.
Jul 23 '05 #7

P: n/a
Victor Bazarov wrote:
BTW is it _require_ to '?:' return value?


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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Jul 23 '05 #8

P: n/a
Dietmar Kuehl wrote:
Victor Bazarov wrote:
BTW is it _require_ to '?:' return value?


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.


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
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.