Search code examples
c++castingsmart-pointersauto-ptr

Casting auto_ptr to void pointer


I am trying to cast auto_ptr to void pointer in the following manner:

void *AM::This2Ctx(std::auto_ptr<AMContext> data)
{
 return reinterpret_cast<void *>(data);
}

but i keep getting a compilation error:

error: invalid cast from type std::auto_ptr<AMContext> to type void*

how can this casting be done correctly? and how can it be used in the opposite way?


Solution

  • Use .get() to access the pointer held by auto-ptr :

    reinterpret_cast<void *>(data.get());
                                 ~~~~~~
    

    In addition, auto_ptr is deprecated, use unique_ptr instead.