0

While debugging someone else's code, I ran into an expression of the following form:

auto result = value == bool_value? result_if_bool_value: result_if_not_bool_value 

I am familiar with ternary operators, but I am confused by the double equality operator in the above statement.

Specifically, what is the equivalent of the above command written with if-else statements?

0

4 Answers 4

3

The conditional operator has lower precedence than ==. Hence the line is parsed as:

auto result = (value == bool_value ) ? result_if_bool_value: result_if_not_bool_value 

It is the usual

( condition ) ? expr_true : expr_false 

In general the conditional operator is not equivalent to an if, but you get the same result with

T result; // auto does not work here !! if ( value == bool_value ) result = result_if_bool_value; else result = result_if_not_bool_value; 

or if you want to keep auto:

auto result = result_if_not_bool_value; if (value == bool_value) result = result_if_bool_value; 

Though, depending on the actual types involved this might do something entirely different than the original (result_if_not_bool_value is here evaluated independent of the condition and there is one initialization and potentially one assignment compared to only initialization. I could swap result_if_not_bool_value with result_if_bool_value, but then the condition would need to be negated which in all generality might result in different result. Though this is all just being super defensive, when the types involved behave "normal", it is mainly a matter of style.)

Sign up to request clarification or add additional context in comments.

Comments

1

The statement

auto result = value == bool_value? result_if_bool_value : result_if_not_bool_value; 

is grouped as

auto result = ((value == bool_value) ? result_if_bool_value : result_if_not_bool_value); 

It's not possible for me to trivially reformulate this into the equivalent if statement due to your use of auto. The type of the ternary conditional expression is the common type of result_if_bool_value and result_if_not_bool_value.

If the types of result_if_bool_value and result_if_not_bool_value were T say, and T has an assignment operator, then the equivalent if block would be

T result; if (value == bool_value){ result = result_if_bool_value; } else { result = result_if_not_bool_value; } 

Comments

1

Fully equivalent code would be (C++17 onward)

auto maker = [&](bool condition) { if ( condition ) return r1; else return r2; }; auto result = maker(value == bool_value); 

Although a good argument could be made that it is harder to understand lambdas and RVO than it is to understand the conditional operator .

Comments

0

If value == bool_value evaluates to true, result is assigned result_if_bool_value else result is initialized result_if_not_bool_value.

1 Comment

result is initialized, not assigned

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.