I want to write a function that modifies a given pointer of any type; thus I declared my function to accept a void*&
, relying on the implicit conversion from any pointer to void*
. However the following code refuses to compile, saying it can't convert int*
to void*&
.
void f(void*& x)
{
x = 0;
}
int main() {
int* a = new int;
f(a);
delete a;
return 0;
}
Note that it works fine if f is declared as accepting an int*&
(but then loses its generality) or if f is declared as accepting a void*
( but then f can only modify its argument locally).
So independently the "any T*
to void*
" implicit conversion rule works, the "T
to T&
implicit conversion rule works, but not both at the same time ? Why is that so ? What did I got wrong here ?
(I know I could use a template function for f, this is mostly out of curiosity).