How does this work in c++? [duplicate] - c++

This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 9 years ago.
I ran into a situation like this:
if(true,false)
{
cout<<"A";
}
else
{
cout<<"B";
}
Actually it writes out B. How does this statement works? Accordint to my observation always the last value counts. But then what is the point of this?
Thanks

from http://www.cplusplus.com/doc/tutorial/operators/
The comma operator (,) is used to separate two or more expressions
that are included where only one expression is expected. When the set
of expressions has to be evaluated for a value, only the right-most
expression is considered.
For example, the following code: a = (b=3, b+2);
would first assign the value 3 to b, and then assign b+2 to variable
a. So, at the end, variable a would contain the value 5 while variable
b would contain value 3.
So here
if(true,false)
{
}
evaluates to if(false)

According to http://www.cplusplus.com/doc/tutorial/operators/
The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.
So for example consider the following:
int a, b;
a = (b=3, b+2);
b gets set to 3, but the equals operator only cares about the second half so the actual value returned is 5. As for the usefulness? That's conditional :)

The comma operator will run whatever is on the left side of the comma, discard it, and then run whatever is on the right side of the operator. In this case:
if (true, false)
will always be equivalent to if (false), so it will never run the if condition, and will always run the else condition.
As a side note: Never write code like this. It is serving no purpose but to obfuscate the code.

Related

Why use this comma in this return statement? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
C++ -- return x,y; What is the point?
(18 answers)
Closed 6 years ago.
I understand what this C++ function does, but I don't understand why the return statement is written this way:
int intDivide(int num, int denom){
return assert(denom!=0), num/denom;
}
There is only one statement here, because there is only one ; but the comma confuses me. Why not write:
int intDivide(int num, int denom){
assert(denom!=0);
return num/denom;
}
Aside from "elegance" is there something to be gained in the first version?
What exactly is that comma doing anyway? Does it break a single statement into 2 parts such that essentially the above 2 versions are identical?
Although the code didn't seem to use constexpr, C++11 constexpr functions were constrained to have only one statement which had to be a return statement. To do the non-functional assertion and return a value there would be no other option than using the comma operator. With C++14 this constraint was removed, though.
I could imagine that the function was rewritten from a macro which originally read something like this
#define INT_DIVIDE(nom,denom) (assert(denom != 0), nom/denom)
The built-in comma operator simply sequences two expressions. The result of the expression is the second operand. The two functions are, indeed, equivalent. Note, that the comma operator can be overloaded. If it is, the expressions are not sequenced and the result is whatever the overload defines.
In practice the comma operator sometimes comes in quite handy. For example, it is quite common to use the comma operator when expanding a parameter pack: in some uses each of the expansions is required to produce a value and to avoid void results messing things up, the comma operator can be used to have a value. For example:
template <typename... T>
void g(T const& arg) {
std::initializer_list<bool>{ (f(arg), true)... };
}
This is a sort of 'syntactic sugar', which is expanded on in a similar question.
Basically the e1, e2 means evaluate e1, and then evaluate e2 - and the entire statement is the result of e2. It's a short and obfuscated (in my opinion) way of writing what you suggest. Maybe the writer is cheap on code lines.
From the C++ standard:
5.19 Comma operator [expr.comma]
1 The comma operator groups left-to-right.
expression:
assignment-expression
expression , assignment-expression
A pair of expressions separated by a comma is
evaluated left-to-right; the left expression is a discarded- value
expression (Clause 5).87 Every value computation and side effect
associated with the left expression is sequenced before every value
computation and side effect associated with the right expression. The
type and value of the result are the type and value of the right
operand; the result is of the same value category as its right
operand, and is a bit-field if its right operand is a glvalue and a
bit-field. If the value of the right operand is a temporary (12.2),
the result is that temporary.
Yes, the two versions are identical, except if the comma operator is overloaded, as #StoryTeller commented.

What is the "?" and ":" sequence actually called? [duplicate]

This question already has answers here:
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 9 years ago.
This may be a bonehead question, but I cannot figure out what the ? exp : other_exp sequence is called.
Example:
int result = (true) ? 1 : 0;
I've tried using the Google machine, but it's hard to Googilize for something without knowing what it's called.
Thanks!
It is called the the conditional operator or alternativly the ternary operator as it a ternary operator (an operator which takes 3 operands (arguments)), and as it's usually the only operator, that does this.
It is also know as the inline if (iif), the ternary if or the question-mark-operator.
It is actualy a rather useful feature, as they are expressions, rather than statements, and can therefore be used, for instance in constexpr functions, assigments and such.
The C++ Syntax is;
logical-or-expression ? expression : assignment-expression
It's used as;
condition ? condition_is_true_expression : condition_is_false_expression
That is, if condition evaluates to true, the expression evaluates to condition_is_true_expression otherwise the expression evaluates to condition_is_false_expression.
So in your case, result would always be assigned the value 1.
Note 1; A common mistake that one makes while working with the conditional operator, is to forget that it has a fairly low operator precedence.
Note 2; Some functional languages doesn't provide this operator, as they have expression 'if...else' constructs, such as OCaml;
let value = if b then 1 else 2
Note 3; A funny use case, which is perfectly valid is using the conditional operator, to decide, which of two variable to assign a value to.
(condition ? x : y) = 1;
Notice the parentheses are necessary, as this is really what you get without them;
condition ? x : (y = 1);
They are called shorthand if-else or ternary operators.
See this article for more information.

Why does putting parentheses around a list of comma separated values change the assignment? [duplicate]

This question already has answers here:
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 9 years ago.
Please consider following code,
int i;
i = 1,2,3,4,5;
printf("First time i = %d\n",i);
i = (1,2,3,4,5);
printf("Second time i = %d\n",i);
Output:
First time i = 1
Second time i = 5
Why do the parentheses make the comma operator take last value and without parentheses it takes first value?
Thanks in advance.
First one is equivalent to (i = 1),2,3,4,5; which means the commas have no effect. When used with parentheses it returns the last value in the "comma delimited list"
This is all due to operator precedence, which you can view a table about here
This is due to the operator precedence and the order of evaluation. = binds harder than , and from that we can figure out that the below two expressions are the same:
i = 1,2,3,4,5 /* <- same as -> */ (i = 1),(2),(3),(4),(5)
side-note: the comma operator is the "weakest" operator of them all
Why does the comma operator yield the last value of our list?
To put it simple this operator evaluate the first operand only to discard it and move on to the next one, it binds left-to-right which means that it will start from the left, and continue walking towards the right.
Where can I read more about this topic?
cppreference.com - C++ Operator Precedence
swansontec.com - C Language Operator Precedence
Assignment has higher precedence than comma , hence the result you get in the first case. You can find the entire operator precedence table here .
Why parenthesis makes comma operator to take last value and without parenthesis it takes first value?
Because parenthesis is used to override the precedence. The first case is implicitly equivalent to :
(i = 1),2,3,4,5;
Comma evaluates from left to right and the rightmost value is the value of the entire expression. Read the documentation here.
The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.
Because = has a higher precedence than , (which has the lowest), the first is the same as
(i = 1),2,3,4,5;
which assigns 1 to i (i = 1) then evaluating the expressions 2, 3, 4, and 5 through the comma operators (the whole expression actually results in 5, which is not used). In the second one,
(1,2,3,4,5)
is parenthesized, therefore it will be first evaluated before =. It results in 5 (the right-most expression; this is the behavior of the comma operator) which is then assigned to i.
i = (1,2,3,4,5);
| |
\---------\--- results in 5 then is assigned to i
See operator precedence Wikipedia article.

How do operators operate in array declarations?

Why does int a[x,y] convert into a[y], since comma operator operates left to right? I would expect a[(x,y)], since inner operation will finish first. But in the first one it is supposed to take the first argument.
I'm not planning to use the comma operator for array initialization, just asking why this happens.
I read it in a book, and I'm confused.
Update:
Wikipedia says:
i = a, b, c; // stores a into i
i = (a, b, c); // stores c into i
So as first line of code says in the array the first value must be assigned to the array. Note: I'm not actually planning to use this. I'm just asking. I'm learning C++ and I read in a book that in an array declaration a[y,x]; so it should be a[y], x; not a[x]. Why does the compiler do this?
The comma operator , is also known as the "forget" operator. It does the following:
Completely evaluate the left operand
Forget its value
Completely evaluate the right operand
Use value of right operand as value of entire operator expression.
So in your case, it behaves just as it should. a[x, y] first evaluates x, then discards its value, then uses the value of y as the value of the entire expression (the one in brackets).
EDIT
Regarding your edit with Wikipedia. Note that the precedence of , is less than that of =. In other words,
i = a, b, c;
is interpreted as
(i = a), b, c;
That's why a is copied into i. However, the result of the entire expression will still be c.
I believe that your compiler is assuming that you meant to add parentheses. In C++ there's a comma operator and a comma separator. The comma operator must itself and its operands be enclosed in parentheses. The int array constructor only expects one value, so I'm guessing that your compiler is trying to help you out.
http://msdn.microsoft.com/en-us/library/zs06xbxh(v=vs.80).aspx
EDIT: int a[x,y] is not valid; int a[(x,y)] is valid; his compiler is assuming he meant to add parentheses. In a more general context the comma operator doesn't require parentheses. In function calls and initializers parentheses are required to distinguish between using a comma operator and a comma separator.

Multiple postfix increment operator in an expression[C++] [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
I'm having my lecturer class right now and my lecturer mention an expression as follows:
int a , b;
a = 4;
b = a++ + a--;
The Question
My lecturer says this expression value can be defined: that it is 8. Which means both a value 4 is summed and assign to b before it's incremented and decremented.
But to me, I think this expression answer is kinda vague and the result will be based on the compiler implementation. That's because to me, the compiler might do the a++ part first—that is, use the value 4 and incremented the a to 5, after that the expression is 4 + 5 = 9 and is assigned to b then only the a is decremented.
It might also do the a-- part first by using the value 4 and decremented it by 1 to 3, and later on sum the value 4 with 3 and assign the value to b then only incremented the a back to 4.
My question is who's correct, my lecturer or me? Because I don't wanna get confused with it and later on will be a problem for me.
Your lecturer is wrong. Writing to the same variable twice without an intervening sequence point is undefined behaviour. From the spec, J.2 Undefined behaviour:
Between two sequence points, an object is modified more than once, or is modified and the prior value is read other than to determine the value to be stored (6.5).
The reference is to 6.5 Expressions, paragraph 5:
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
Annex C of the C spec has a handy list of all of the sequence points:
The following are the sequence points described in 5.1.2.3:
The call to a function, after the arguments have been evaluated (6.5.2.2).
The end of the first operand of the following operators: logical AND && (6.5.13); logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17).
The end of a full declarator: declarators (6.7.5);
The end of a full expression: an initializer (6.7.8); the expression in an expression statement (6.8.3); the controlling expression of a selection statement (if or switch) (6.8.4); the controlling expression of a while or do statement (6.8.5); each of the expressions of a for statement (6.8.5.3); the expression in a return statement (6.8.6.4).
Immediately before a library function returns (7.1.4).
After the actions associated with each formatted input/output function conversion specifier (7.19.6, 7.24.2).
Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call (7.20.5).