tags:

views:

1469

answers:

10

for the following:

( a != b ) ? cout<<"not equal" : cout<<"equal";

suppose I don't care if it's equal, how can I use the above statement by substituting cout<<"equal" with a no-op.

+1  A: 

This is very confusing code. You could just write

cond ? cout << "equal" : cout;

but you won't (will you?) because you've got conventional if for that.

Konrad Rudolph
+15  A: 

If it really is for a ternary operator that doesn't need a second action, the best option would be to replace it for an if:

if (a!=b) cout << "not equal";

it will smell a lot less.

Vinko Vrsalovic
That doesn't work if it is used as an expression. "if" is not an expression in C.
Hugh Allen
So? The idea is exactly avoid doing that kind of ugly expression in the first place.
Vinko Vrsalovic
A: 
if (a!=b) cout<<"not equal";
Chris Lively
+10  A: 

Simple: I would code it as

if (a != b)
   cout << "not equal";

The ternary operator requires the two results to be of the same type. So you might also be able to get away with

(a != b) ? cout << "not equal" : cout;

because the stream operator (<<) just returns the ostream reference. That's ugly and unnecessary in my opinion though.

altruic
+1  A: 

I think the problem here is that the operator : has two EXPRESSIONS as arguments. Let's say.. a = x ? y : z;

Expression by definition must have a value...that's why you cannot just "skip".

badbadboy
+4  A: 

The only thing missing from the other answers is this: There is no way, directly, to code a "noop" in C/C++.

Also, doing: (a != b) ? : printf("equal\n"); does actually compile for me (gcc -ansi in gcc 4.2.4).

SoapBox
This isn't entirely true. An empty block may act as “no-op”. However, this isn't an *expression*. Since an expression must be typed in C++, there's no literal/constant that is universally acceptable as “no-value”. “no-op” isn't really the right word here.
Konrad Rudolph
A: 

The syntax just requires a expression. You can just go: (a!=b)?cout<<"not equal":1;

what is the resultant type of that expression? Int or Stream?
BCS
won't work: standard: (C ? E1 : E2) in the following: if E1 or E2 has a nonclass type, E1 can be converted to match E2 if E1 can be implicitly converted to the type that expression E2 would have if E2 were converted to anrvalue (or the type it has, if E2 is an rvalue).)
Johannes Schaub - litb
+2  A: 

The following will achieve what you're looking for, however, it may not be clear to people reading your code why it works:

(a != b) && (cout << "equal");

Personally, I agree with this answer from Vinko Vrsalovic.

Richard Corden
That's interesting hack, but for readability reasons I'd recommend not to use it. Also, you shouldn't rely on compiler optimalization.
ya23
+1  A: 

If the focus of the code is the output operation and not the condition, then something like this could be done:

cout << (cond ? "not equal" : "");

I suspect that's not the case, though, because you want to do nothing in the "else" clause.

Greg D
A: 

Both statements compile:

( a != b ) ? cout<<"not equal" : NULL;

( a != b ) ? NULL : cout<<"equal";