Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using std::shared_ptr<void> to point to anything

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.

like image 893
Bruce Avatar asked Mar 26 '14 07:03

Bruce


2 Answers

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).

like image 112
filmor Avatar answered Sep 28 '22 20:09

filmor


If you store void*, then at every use point you need to know the exact type you put in, as a cast-to-void*-and-back is only valid if you go to/from the exact same type (not derived-to-void-to-base, for example).

Given that every use point will require knowing the type of the stored pointer, why round trip to void at all?

Have one map per type you store. This has modest overhead over one map per application (O(number of types) memory at run time, similar compile time).

With C++ templates there will be little code duplication.

like image 37
Yakk - Adam Nevraumont Avatar answered Sep 28 '22 19:09

Yakk - Adam Nevraumont