[ACCEPTED]-Parallel assignment in C++-c++
That's not possible. Your code example
a, b = b, a;
is 8 interpreted in the following way:
a, (b = b), a
It does 7 nothing. The comma operator makes it return 6 the value of a (the right most operand). Because 5 assignment binds tighter, b = b is in parens.
The 4 proper way doing this is just
std::swap(a, b);
Boost includes 3 a tuple class with which you can do
tie(a, b) = make_tuple(b, a);
It internally 2 creates a tuple of references to a and b, and 1 then assigned to them a tuple of b and a.
Parallel assignment is not supported in 5 C++. Languages that support this usually 4 treat a,b,c
as a list on either side of the assignment 3 operator, this isn't the way the comma operator 2 works in C++. In C++, a, b
evaluates a and 1 then b, so a, b = b, a
is the same as a; b = b; a;
.
Or Perl. But no, it's not possible (as far 6 as I'm aware), you need to use a temporary 5 variable, as in:
int a = 4;
int b = 5;
{
int tmp = a;
a = b;
b = tmp;
}
FYI, internally those languages 4 (or Perl atleast) create a temporary list 3 { a, b }
, then assign the list to the two variables. In 2 other words, this is atleast as performant, if 1 only a little more messy.
This question is very old – I 25 just stumbled into it today...
...and wondered 24 why nobody gave this answer before...
I think, it's 23 possible to do it in C++11 similar like 22 Python does it (under the hood):
#include <iostream>
using namespace std;
int main()
{
int a = 4, b = 5;
cout << "Before assignment: a: " << a << ", b: " << b << endl;
pair<int&, int&> ba(b, a);
ba = make_pair(a, b); // <===: (b, a) = (a, b)
cout << "After assignment : a: " << a << ", b: " << b << endl;
return 0;
}
I tried 21 this out on ideone.com. The output was:
Before assignment: a: 4, b: 5
After assignment : a: 5, b: 4
If I remember 20 right (I'm not a Python expert), in Python, a, b
denotes 19 a pair. (Python Doc.: 5.3. Tuples and Sequences)
Such pair can be done in C++ 11 18 easily e.g. with std::pair
. In this case, I made 17 a pair of references and assigned the pair 16 of values. It works as the make_pair()
loads both variables 15 before the right pair (of values) is assigned 14 to the left pair of references.
Scrolling 13 again, I realize that this answer is close 12 to the boost based solution of Johannes answer.
May be, the 11 reason is that it didn't work in C++03. I 10 tried in coliru.stacked-crooked.com: With 9 -std=c++03
it yields terrible to read compiler errors 8 – changing to -std=c++11
and it compiles 7 and executes fine as described above.
Disclaimer
I just 6 cannot imagine what this solution is good 5 for nor what practical worth it may have. This 4 is not what I tried to do. As many other 3 answers states "It does not work." IMHO 2 it does (spelling it right according to 1 the C++ language)...
Or Lua...
There are tricks with C/C++, like 3 using xor or operations, but with risk of 2 overflow and such. Just do it the painful 1 way, with three assignments. Not a big deal.
There is no such function in the Standard 3 Library. You could write a set of template 2 functions :
template <typename T1> void ParAssign(T1& Lhs_1, T1 const& Rhs1);
template <typename T1, typename T2> void ParAssign(T1& Lhs1, T2& Lhs2, T1 const& Rhs1, T2 const& Rhs2);
// etc.
ParAssign(a,b,
b,a);
That's non-trivial if there is 1 aliasing, as in your swap example.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.