Random Suggestion: Swap Operator <=>?

Ary Borenszweig ary at esperanto.org.ar
Fri Jul 17 08:22:38 PDT 2009


Julian Salazar wrote:
> I'm wondering, who here would use a swap operator if it were available?
> 
> Something that would be normally achieved through:
> temp = a;
> a = b;
> b = temp;
> or for the more bit-wise ;)
> a ^= b;
> b ^= a;
> a ^= b;
> 
> It's something I've never actually seen implemented in any higher level 
> language, but finds uses in linked lists, binary trees, and other data 
> structures and algorithms. Temp wouldn't need to be an addressable 
> value, so theoretically this operator (which I propose to be "<=>") 
> could be compiler-optimized into an x86 xchg opcode for example. It 
> could properly deal with references, data structures, class instances, 
> etc. and not require the programmer to worry about these details.
> 
> I know that the chance of it actually being implemented is close to nil, 
> but even a template in the standard library for it would be cool (or did 
> I miss something?). Or am I being deluded? :)

Oh, come on, It's just three lines of code! And it's not *that* common. 
I know it's kind of basic and primitive, but there's a standard library 
function for it. Why add an operator *just* for that?

The tuple assignment suggestion is better because it can handle more 
interesting cases.



More information about the Digitalmars-d mailing list