Is there any way of doing parallel assignment in C++? Currently, the below compiles (with warnings)
#include <iostream> int main() { int a = 4; int b = 5; a, b = b, a; std::cout << 'a: ' << a << endl << 'b: ' << b << endl; return 0; }
and prints:
a: 4 b: 5
What I’d like it to print … if it weren’t obvious, is:
a: 5 b: 4
As in, say, ruby, or python.
That’s not possible. Your code example
is interpreted in the following way:
It does nothing. The comma operator makes it return the value of a (the right most operand). Because assignment binds tighter, b = b is in parens.
The proper way doing this is just
Boost includes a tuple class with which you can do
It internally creates a tuple of references to a and b, and then assigned to them a tuple of b and a.