Reputation: 3200
I'm confused about direct assignment and ternary conditional operators precedence:
#include<stdio.h>
int main(void)
{
int j, k;
j = k = 0;
(1 ? j : k) = 1; // first
printf("%d %d\n", j, k);
j = k = 0;
1 ? j : k = 1; // second
printf("%d %d\n", j, k);
return 0;
}
I would expect the output to be:
1 0
1 0
But it happens to be:
1 0
0 0
Plus I get this warning:
main.cpp:20: warning: statement has no effect
which is about the line I commented as second.
Since the direct assignment operator has less precedence than the ternary conditional operator, I was expecting lines commented as first and second to be equivalent. But alas it is not the case.
I tried this with g++ --version (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Upvotes: 35
Views: 9742
Reputation:
By the precedence rules of C++ (https://en.cppreference.com/w/cpp/language/operator_precedence),
1 ? j : k = 1;
is evaluated right-to-left, i.e.
1 ? (j) : (k = 1);
[also see note 2 in the link].
This indeed has no effect as only the intermediate expression is performed.
Upvotes: 0
Reputation: 141
Previous answers explain the results, but they don't clear the doubt. I'll try to give my understanding. If there are any mistakes, please point it out.
Compilers may analyze and generate code as follows:
Parse the plain code to a tree (code plain text -> semantic unit).
Original code break down to minimal semantic unit and format the tree. In the process, compiler need to know each operator's precedence. And according to C++ operator precedence, we know
?:
(conditional operator) has same precedence with =
(direct assign operator), precedence = 16k = 1
higher than ?:
.So for code 1 ? j : k = 1;
, the tree may like this:
op(?:)
/ | \
/ | \
[condition] [true-branch] [false-branch]
1 j op(=)
/ \
k 1
Decide the Eval order (generate the intermediate representation)
To generate runnable code, the compiler needs to know which part should eval(run) firstly. According to the Order of evaluation, we know for operator ?:
, it should firstly evaluate [condition]
, then evaluate according to the condition result. It is straightforward and follows our intuitive.
So imagine the final running process, 1
is eval to true, and true branch j
then evaluated, k = 1
will never run.
So after knowing 1,2
, we know the fully reason.
As the cpp document, step
1
is incompile-time
concept, while step2
is inruntime
concept. But from the compiler's view, they are all in compiling process, and thecompile-time/runtime
concept is just from the language-design view.
Upvotes: 0
Reputation: 34393
The operator precedence in the C/C++ language in not defined by a table or numbers, but by a grammar. Here is the grammar for conditional operator from C++0x draft chapter 5.16 Conditional operator [expr.cond]:
conditional-expression: logical-or-expression logical-or-expression ? expression : assignment-expression
The precedence table like this one is therefore correct when you use assignment on the left side of the doublecolon, but not when used on the right side. What is the reason for this asymmetry I have no idea. It may be a historical reason: in C the conditional result was not lvalue, therefore assigning something to it had no sense, and allowing assignment to be accepted without parentheses might seem smart at that time.
Upvotes: 23
Reputation: 476950
The second line is equivalent to:
1 ? (j) : (k = 1);
That's the same as:
j;
That's the same as:
;
The key is that the two operands of the ternary conditional operator can be expressions, so operator precedence isn't relevant here. It's simply that the second operand is the assignment expression k = 1
.
Upvotes: 14
Reputation: 69978
(1 ? j : k) = 1;
is equivalent to,
if(true) j = 1;
else k = 1;
And,
1 ? j : k = 1;
is equivalent to,
if(true) j; // warning: statement has no effect
else k = 1;
Upvotes: 9
Reputation: 7272
In the second case,
1 ? j : k = 1;
is evaluated as:
(1) ? (j) : (k = 1);
and since one evaluates to true
, the expression evaluates to j
which does nothing.
Upvotes: 3