Unsure what this while statement is doing [duplicate] - c++

This question already has answers here:
How does the Comma Operator work
(9 answers)
Expression "variable, variable = value;"
(4 answers)
Closed 9 years ago.
So, I'm doing homework. I've encountered something I haven't seen before and cannot find a decent explanation of what it does. Basically,
Object object;
...
while((value1, value2) = function(object)) {
object.foo(value1, value2);
}
The (value1, value2) in the while statement really throws me. Any ideas?

Its a comma operator.
The result of the comma operator is the last value (the others are evaluated and discarded).
while((value1, value2) = function(object)) {
object.foo(value1, value2);
}
If value1 is just a variable and not an expression then it is equivalent too:
while(value2 = function(object)) {
object.foo(value1, value2);
}
If value1 is an expression then it is evaluated each time around the loop. The result is discarded, but if the expression has side effects these will take effect.

Related

What does ?: mean in C++? [duplicate]

This question already has answers here:
?: ternary conditional operator behaviour when leaving one expression empty
(2 answers)
Closed 2 years ago.
I found this piece of code:
std::string(avdInfo_getSystemImagePath(m_avd)
?: avdInfo_getSystemInitImagePath(m_avd))
I only found information about the conditional operator: http://www.cplusplus.com/articles/1AUq5Di1/
That is, ? and : are separated. But what does it mean when they are together? both avdInfo_getSystemImagePath and avdInfo_getSystemInitImagePath return char*
It's a GCC extension.
x ?: y
is the same as
x ? x : y
Except that x will only be evaluated once.

Is there any guarantee to be sure the left side of && operator is always evaluated first? [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 2 years ago.
As the title says: Is there any guarantee from C++ Standard to be sure the left side of && (or and) operator always evaluated first? To be honest, I couldn't search in C++17 Standard, I don't know which section I most look for.
Sample of Problem:
I want to do something like this:
std::unordered_map<std::size_t, std::weak_ptr<T>> objects;
bool f (std::size_t const id) {
bool result = false;
if (not objects.at(id).expired()) {
auto const& object = objects.at(id).lock();
/* Here left side of `and` most be evaluated first */
result = object->parent->remove(id) and
objects.erase(id) == 1;
}
return result;
}
And want to be sure there is no problem with the code.
[expr.log.and]/1 ... Unlike &, && guarantees left-to-right
evaluation: the second operand is not evaluated if the first operand is false.

Array not assignable [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 3 years ago.
I wanted to assign a boolean value to a two-dimensional boolean array, but the compiler showed an error
bool Amass[100][80];
Amass[1,1] = true; //even so I see only an error
You have declared a two-dimensional array
bool Amass[100][80];
However in this statement
Amass[1,1] = true;
in the subscript operator expression you are using the comma operator. Its result is the right-most operand. That is the statement is equivalent to
Amass[1] = true;
So in the left side of the assignment there is used a one-dimensional array.
It seems you mean
Amass[1][1] = true;

Why when using ternary operator with std::cout, the conidtion gets printed instead of the result? [duplicate]

This question already has answers here:
C++, ternary operator and cout
(2 answers)
Conditional operator used in cout statement
(3 answers)
Closed 2 years ago.
When I write
std::cout << 5 ? 'a' : 'b';
the output is 5? Why should I put the whole expression in parentheses? isn't the ternary operator as a whole an expression and should be evaluated first then output the result? And if std::cout consider that I wanna print the condition, what happens to the part after the condition? Why is it ignored?

How does "()" convert statements into expressions in C++? [duplicate]

This question already has answers here:
Are compound statements (blocks) surrounded by parens expressions in ANSI C?
(2 answers)
Closed 4 years ago.
I have the following code:
int main() {
int i=0;
int j=({int k=3;++i;})+1; // this line
return 0;
}
It compiles and runs. If I remove the () from "this line", then it doesn't compile.
I'm just curious what syntax rule is being applied here.
The {} contains 2 statements, and the last statement indicates the "return" value of this code block. Then why does it need an extra () pair to make this return value usable?
That's a statement expression, and it's a GCC-specific extension.
From the linked reference:
A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.
A compound statement is a curly-brace enclosed block of statements.