r/cpp•
notarealoneatall

Is it possible to use generics to create a container that can hold any type?

Removed - Help

I recently came up with this design to map all of my drivers (work) and threads within a single object using a map of `<key_t, void*>`, where the `void *` is a hash map of `<void *, void *>`. This works fine for my needs, but I am wondering if there's a way to do this with better use of generics. The only thing I can't figure out is that it seems like if I introduce template params for the maps, it would end up requiring an object per type.

The reason I can't use an object per type is because I don't really want to create an object for every possible type I may encounter at any given time. I would like to just have a single "dispatcher" object that is able to map the drivers and threads and manage that memory. It shouldn't matter what the types are to the dispatcher since it doesn't need to interface with them other than to store them and delete them when they're done.

Posted May 15, 2025, 4:26 PM UTC

050
Loading comments…