473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

operator ?:

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
8 1650
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.