(a, b) = (10, 20)
is allowed code in C++ (not in C). The result is that 20 is assigned to b, and a is untouched.Contrast this to:
a, b = 10, 20
where (as above) 10 is assigned to b and a is left untouched, because it is parsed as: a, (b = 10), 20
and I hope you agree that the behavior is very confusing. #include <tuple>
#include <iostream>
std::tuple<int, int> divide(int dividend, int divisor) {
return {dividend / divisor, dividend % divisor};
}
int main() {
using namespace std;
auto [quotient, remainder] = divide(14, 3);
cout << quotient << ',' << remainder << endl;
} auto [quotient, remainder] = divide(14, 3);
is not an assignment. In an assignment, you should write something like std::tie(quotient, remainder) = divide(14, 3);
which is a tuple assignment written as a single assignment.This shows the kind of (imho) "ugly hacks" the C++ committee had to make to cover up the historical mistake with the comma operator.
(I find this the most weird thing about C/C++ culture: they are so proud it is static typed, yet oblivious to all the other problems and traps/memory unsafety/undefined behavior/etc.)