I had something similar sitting around in a header file... Mine is one line of code though

inline void
UselessSwap(int& a, int &;)
{
a -= b += a -= b = -b;
}

Not that I'm whining or anything, but if my understanding is correct about certain aspects of c++, the above code is going straight for the realm of "undefined behavior" in c++.

Someone correct me if I'm wrong.

oisyn
—
2006-10-11T23:26:15Z —
#19

You're wrong

oisyn
—
2006-10-11T23:39:26Z —
#20

Sorry, I was being a bitch :happy: The reason you're wrong is because the associativity of these expressions is well defined. Just as a + b + c + d is parsed as ((a + + c) + d and std::cout \<\< "hello" \<\< 34 \<\< std::endl is equivalent to ((std::cout \<\< "hello") \<\< 34) \<\< std::endl, this expression becomes a -= (b += (a -= (b = -B)))

Each = operator returns a reference to *this and is used in the outer expression. The thing that isn't defined is the order of evaluation (which is different from associativity) in the function argument list. ++a = a++ is undefined because that translates to operator=(++a, a++) and the outcome depends on which is evaluated first: ++a or a++.