# Thread: condition ? instruction1 : instruction2 ; question.

1. ## condition ? instruction1 : instruction2 ; question.

Hello,
I have such a line:
Code:
`mapa[i][j] == '1' ? dist[i][j] = 0, Q.push(p(make_pair(i,j), make_pair(i,j))) : dist[i][j] = 0x7fffffff ;`
but it doesn't really work, I got an error:
error: `((void)(dist[i][j] <unknown operator> 0), (&Q)->std::queue<_Tp, _Sequence>:ush [with _Tp = p, _Sequence = std::deque<p, std::allocator<p> >](((const p&)((const p*)(&p(std::make_pair [with _T1 = int, _T2 = int](i, j), std::make_pair [with _T1 = int, _T2 = int](i, j)))))))' has type `void' and is not a throw-expression
Can anyone tell me why it doesn't work ?
This part of code which do the same works fine:
Code:
```dist[i][j] = 0x7fffffff;
if(mapa[i][j] == '1')
{
dist[i][j] = 0;
Q.push(p(make_pair(i,j), make_pair(i,j)));
}```
Regards,
apacz

2. If I were you, I would use the latter form and just forget about it. The ternary operator has its uses, but obfuscating code is not one of them.

3. Hi,
I don't think that it obfuscates the code but it makes it shorter.
Regards,
apacz

4. For the sequential evaluation operator to work effectively inside the ternary operator, you have to group expressions inside parenthesis. This is so because of the higher precedence of the ternary operator.

But as Laserlight said, what you are doing is poor, poor, form. It's ugly and it helps no one, not even you the original programmer as you are already realizing.

5. And what exactly is wrong with "long code"?

6. > I don't think that it obfuscates the code but it makes it shorter.
There is no relationship between the number of characters in your source code and the number of instructions generated by the compiler.

You still have the same number of function calls, comparisons and assignments. Rearranging them into some obfuscated mess isn't going to produce any more efficient code.

On the contrary, highly obfuscated code is trickier to optimise than simple code - if it's hard to understand, then chances are the optimiser will have a hard time figuring it out as well and produce worse code.

But if you really want it, then perhaps
Code:
```dist[i][j] = mapa[i][j] == '1'
? Q.push(p(make_pair(i,j), make_pair(i,j))), 0
: 0x7fffffff ;```

7. Thanks for replies.
Salem: I have a question to the middle part of this operator (Q.push(p(make_pair(i,j), make_pair(i,j))), 0). .push() doesn't return anything so this '0' is a real value of dist[][]. But how does it really work, I mean does this ternary operator looks for first available value to assign ? for example tmp = something == 1 ? Q.push(0), Q.push(1), 0, Q.push(2) : 0 ;
And if the something == 1, then tmp will be equal to 0 ?
Regards,
apacz

8. Look up the comma operator.
a = foo, bar;
The result is the right hand side of the comma operator, but the left hand side is also evaluated (for any side effects).

> assign ? for example tmp = something == 1 ? Q.push(0), Q.push(1), 0, Q.push(2) : 0 ;
> And if the something == 1, then tmp will be equal to 0 ?
No, it will be the result returned by Q.push(2)

9. Thanks.