Xor trick?

bearophile bearophileHUGS at lycos.com
Sat Jan 25 16:04:06 PST 2014


Do you know how to perform the xor trick 
(http://en.wikipedia.org/wiki/XOR_swap_algorithm ) on two 
pointers in D?

This is a try:


void foo(T)(ref T x, ref T y) pure nothrow {
     x ^= y;
     y ^= x;
     x ^= y;
}
void main() {
     int* p, q;
     foo(p, q);
}


Currently that gives:

test.d(2): Error: 'x' is not of integral type, it is a int*
test.d(2): Error: 'y' is not of integral type, it is a int*
test.d(3): Error: 'y' is not of integral type, it is a int*
test.d(3): Error: 'x' is not of integral type, it is a int*
test.d(4): Error: 'x' is not of integral type, it is a int*
test.d(4): Error: 'y' is not of integral type, it is a int*
test.d(8): Error: template instance test.foo!(int*) error 
instantiating

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list