Why is this not acceptable?

bool get_player_turn() const { p1_turn ? return true : return false; }

Question in the title.
Last edited on
It has the same effect of just returning p1_turn.

If you were to use that statement, it would be:
return p1_turn ? true : false;
hmmmm ok, i didn't realise you could use return in that way.

And sorry, should have mention before that p1_turn is of type bool.
As to why that is not acceptable.

The ternary operator ?: is an operator, not a control structure like if()else. While they may be functionally equivalent in select cases, that doesn't mean they are universally interchangeable.

"return true" is a statement, not an expression. Expressions can be used as the return value (so long as they resolve to the expected type being returned), but a return statement cannot be used as an expression within an operation because it performs a specific action, which is to exit the current body of code, passing control back to the calling body of code.

that action can be performed within the body of a control structure like if()else, but it cannot be performed in the middle of an operation.
Thank you.
You can do
bool get_player_turn() const { return p1_turn ? true : false; }
I already said that. The least stupid method this:

 
bool get_player_turn() const { return p1_turn; }
It's not stupid at all, and is routine in C. My version is sometimes used to stop the compiler from complaining about a narrowing conversion (of int to bool).
1. It's not C.
2. p1_turn is bool.
Topic archived. No new replies allowed.