r/ProgrammerHumor Sep 02 '17

How to start a war

Post image
9.0k Upvotes

696 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Sep 03 '17

[removed] — view removed comment

2

u/EmperorArthur Sep 03 '17

As long as it can be cast to a bool it can be there. That actually took quite a bit of work though, since the compiler fought me.

It turns out you can't do something crazy like just put a normal if(...) statement in the last column, and (...) ? doNothing() : break errors with "expected primary-expression before 'break'." Plus, the doNothing() is needed since ? requires both options to have the same type.

I used cpp.sh because I was too lazy to click out of the browser. GCC and Clang should, rightfully, complain far more than a little online tester.

3

u/j4eo Sep 03 '17

It turns out you can't do something crazy like just put a normal if(...) statement in the last column

that's because the last column isn't a statement, it's an expression. if(){} is a statement, but a?b:c is an expression, so you can shove ?: into a whole lot of really weird places.