C++ – Using std::shared_ptr to point to anything

cc++11shared-ptrsmart-pointersstl

I'm using a std::shared_ptr<void> in my application to make a smart pointer which can point to many different types of data structures like structs, vectors, matrices… basically anything. What I'm trying to do is map some names to their data structures.

I'm performing the mapping using a hashtable:

std::unordered_map<std::string, std::shared_ptr<void>>

Can I cast the std::shared_ptr<void> returned by find() back to std::shared_ptr<my_type>? If so, how?

More importantly, is this good practice? Will this increase the complexity too much as the application scales? Or, is there some other completely different, elegant approach?

EDIT

Probably cannot use `Boost.Any' since it uses RTTI.

Also cannot use a base class for all these data structures, since some of them are STL containers like std::vector.

About the shared_ptr delete issue discussed on an answer below, I read that shared_ptr performs type erasure and does store type information to know which destructor to call.

Shared void pointers. Why does this work?

Why do std::shared_ptr<void> work

Why is shared_ptr<void> not specialized?

But I'm not sure about this.

Best Answer

This is not good practice. If you don't store additional type information next to your std::shared_ptr (which you can cast using static_pointer_cast) you have undefined behaviour all around. Maybe Boost.Any is an option for you?

If you want to stick with std::shared_ptr<void> please remember to provide a custom deleter function (see make shared_ptr not use delete).