views:

331

answers:

5

I was always wondering why such a simple and basic operation like swapping the contents of two variables is not built-in for many languages.

It is one of the most basic programming exercises in computer science classes; it is heavily used in many algorithms (e.g. sorting); every now and then one needs it and one must use a temporary variable or use a template/generic function.

It is even a basic machine instruction on many processors, so that the standard scheme with a temporary variable will get optimized.

Many less obvious operators have been created, like the assignment operators (e.g. +=, which was probably created for reflecting the cumulative machine instructions, e.g. add ax,bx), or the ?? operator in C#.

So, what is the reason? Or does it actually exist, and I always missed it?

A: 

You do have the XOR operator that does a variable substitution for primitive type...

VonC
+6  A: 

In my experience, it isn't that commonly needed in actual applications, apart from the already-mentioned sort algorithms and occasionally in low level hardware poking, so in my view it's a bit too special-purpose to have in a general-purpose language.

As has also been mentioned, not all processors support it as an instruction (and many do not support it for objects bigger than a word). So if it were supported with some useful additional semantics (e.g. being an atomic operation) it would be difficult to support on some processors, and if it didn't have the additional semantics then it's just (seldom used) synatatic sugar.

The assigment operators (+= etc) were supported because these are much more common in real-world programs - and so the syntacic sugar they provide was more useful, and also as an optimisation - remember C dates from the late 60s/early 70s, and compiler optimisation wasn't as advanced (and the machines less capable, so you didn't want lengthy optimisation passes anyway).

Paul

Paul
Nice answer. But there are more algorithms in which it is used: Fisher-Yales algorithm (modern version), Steinhaus-Johnson-Trotter algorithm, Matrix-Tranposition. And in more specialized ones, where it is used as argument for reducing space complexity. I admit that these are specialized cases.
mr_georg
"these are specialized cases"Exactly.
Paul
+2  A: 

It's a widely used example in computer science courses, but I almost never find myself needing it in real code - whereas I use += very frequently.

Yes, in sorting it would be handy - but you don't tend to need to implement sorting yourself, so the number of actual uses in source code would still be pretty low.

Jon Skeet
A: 

I think they just forgot to add it :-) Yes, not all CPUs have this kind of instructions, so what ? We have bunch of other things that most CPUs don't have instructions to compute. It would be much easier/clearer and also faster ( by intrinsic ) if we had it !!!

Malkocoglu
Wouldn't necessarily be any faster - the compiler could inline the call to swap (and I would imagine some already do) if it really is performance critical.
Paul
What about the underlying hardware, if it had XCHG or similar instruction, swap would be done without needing a Temporary variable and it would execute faster. There is more to intrinsic than inlining !
Malkocoglu
...and there's more to optimization than inlining. Compilers are capable of figuring out when a swap is occurring (it's not *that* hard to identify a temp variable and use XCHG instead).
Imagist
+4  A: 

C++ does have swapping.

#include <algorithm>
#include <cassert>

int
main()
{
    using std::swap;
    int a(3), b(5);
    swap(a, b);
    assert(a == 5 && b == 3);
}

Furthermore, you can specialise swap for custom types too!

Chris Jester-Young
This is just a defined function. What I meant was, why it is not directly built-in.
mr_georg