6

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Juba
  • 4,425
  • 3
  • 25
  • 19
  • possible duplicate of [How do I implement no-op macro (or template) in C++?](http://stackoverflow.com/questions/1306611/how-do-i-implement-no-op-macro-or-template-in-c) – user Mar 10 '14 at 20:56

12 Answers12

22

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
  • 330,807
  • 53
  • 334
  • 373
10

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
  • 354
  • 2
  • 7
6

(void)0; is noop. There is a lots of good reason to use expr?false:true form. Look how assert() is implemented.

So in your example, use ( a != b ) ? (void)0 : cout<<"equal";

Regular Guy
  • 81
  • 1
  • 2
5

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
  • 20,457
  • 3
  • 51
  • 87
  • 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 Nov 18 '08 at 21:49
  • 1
    gcc has an extension that allows leaving out the middle term, but it's not legal C or C++. – bames53 Jul 18 '12 at 15:42
4

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.

Community
  • 1
  • 1
Richard Corden
  • 21,389
  • 8
  • 58
  • 85
  • That's interesting hack, but for readability reasons I'd recommend not to use it. Also, you shouldn't rely on compiler optimalization. – ya23 Dec 05 '08 at 01:07
  • 4
    @ya23, could you be more precise about what optimization he relies upon? That expression is well-defined in all optimization contexts. – Robᵩ Sep 12 '11 at 18:28
1

In C++11 you can write ( in case of void ) :

somecondition ? foo() : [] {} () ;

So the NOP is actually an empty lambda. Besides void you could return any type and value.

This might look a bit overkill all by itself but suppose you have this :

somecondition1 ? foo1() :
somecondition2 ? foo2() :
somecondition3 ? foo3() :
                 flip_out_because_unhandled_condition() ;

Now if someone adds somecondition4, but forgets to include it in the handling code, the software will call the flip_out_... function causing all kinds of unwanted effects. But maybe somecondition4 doesn't need any special attention, it just needs to be ignored. Well then you could write :

somecondition1 ? foo1()   :
somecondition2 ? foo2()   :
somecondition3 ? foo3()   :
somecondition4 ? []{}() :
                 flip_out_because_unhandled_condition() ;
QBziZ
  • 3,170
  • 23
  • 24
1

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
  • 530,221
  • 131
  • 937
  • 1,214
1

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

Paul Kapustin
  • 3,297
  • 5
  • 35
  • 45
1

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
  • 43,259
  • 14
  • 84
  • 117
0
if (a!=b) cout<<"not equal";
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
NotMe
  • 87,343
  • 27
  • 171
  • 245
0

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 Nov 18 '08 at 21:50
  • 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 an rvalue (or the type it has, if E2 is an rvalue).) – Johannes Schaub - litb Nov 18 '08 at 21:59
0

Both statements compile:

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

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