1. You're probably breaking strict aliasing once you dereference one of the pointers after swapping.
unsigned int* a;
double* b;
SWAP_POINTERS(a,b);
Don't swap pointers which do not point to the same type however. (Note that char
aliases other types but that is an exception.)
2. You cannot convert int*
to void*&
implicitly.
int*
is implicitly convertible to void*
but the conversion creates an rvalue which cannot be bound to an lvalue reference.
In order to use SWAP_POINTERS
with the signature shown in the question, one would have to create lvalues of type void*
first and pass those to SWAP_POINTERS
.
Example:
int a{0}, b{1};
int * pa = &a;
void * vpa = pa;
int * pb = &b;
void * vpb = pb;
SWAP_POINTERS(vpa, vpb); // swaps vpa and vpb - OF COURSE :)
Please note that this doesn't solve the (probably inteded) swapping of (unrelated) pointers with minimal code. Its just an explanation of why the original code with void*&
in place did not work when passing int*
and how a call to the original function would have to look in order to be able to operate (on its function arguments...).
3. Swap pointers via std::swap
int a{0}, b{1};
int * pa = &a;
int * pb = &b;
std::swap(pa, pb);