question about conditional operator (?:)

Richard via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 26 06:41:39 PDT 2016


On Tuesday, 26 July 2016 at 13:19:54 UTC, ag0aep6g wrote:
> Operator precedence is different from what you think. `a ? b : 
> c = d` means `(a ? b : c) = d`. But you want `a ? b : (c = d)`. 
> So you need parentheses around `p+=1`.
>
> Or just go with `if` and `else`. It's clearer anyway.

 From http://wiki.dlang.org/Operator_precedence

Priority(15 is highest)
3                      Conditional operator                  ?: 	
2                      Assignment operators    = -= += <<= >>= 
 >>>= = *= %= ^= ^^= ~=

I was actually close to not needing parentheses :). But I see 
that your suggestion to stick with if else in this case is the 
sensible thing to do, especially since ?: seems to lead to more 
errors. Thanks for the answers.


More information about the Digitalmars-d-learn mailing list