c++ - Implicit type conversion with reference to void* -
i want write function modifies given pointer of type; declared function accept void*&
, relying on implicit conversion pointer void*
. following code refuses compile, saying can't convert int*
to void*&
.
void f(void*& x) { x = 0; } int main() { int* = new int; f(a); delete a; return 0; }
note works fine if f declared accepting int*&
(but loses generality) or if f declared accepting void*
( f can modify argument locally).
so independently "any t*
void*
" implicit conversion rule works, "t
t&
implicit conversion rule works, not both @ same time ? why ? did got wrong here ?
(i know use template function f, out of curiosity).
it's because reference bit. reference pointer 1 type not same reference pointer of type.
this can of course solved using templates:
template<typename t> void f(t*& x) { ... }
Comments
Post a Comment