For the question, suppose the following code flow:
void * some_data = allocate_memory(const std::string & type) fetch_data_from_somewhere(some_data); send_data_to_client(some_data); allocate_memory will just allocate an empty message, which will be filled (including potential further allocation of dynamic memory) via fetch_data_from_somewhere and then sent somewhere by send_data_to_client. The client then casts the data to the real type (which only he knows about).
I now have to write the function to allocate_memory given only a string identifying the type. With that information, I can get a data struct describing the message (e.g.: It's 80 bytes long, there is an integer in the first 4 bytes, a std::vector afterwards, etc.).
Imagine I know that I have to allocate a std::vector<T> where T is a type unknown to me (again, I know a string identifier of the type and can access information regarding its structure, size, etc.).
If I use void * some_memory = calloc(sizeof(std::vector<T>), 1), this seems to work for all T (except for booleans) with gcc, clang and MSVC++ compilers. Later on, the client can just do static_cast<std::vector<T>>(some_memory) and using all vector operations seems to work. This seems somewhat reasonable, although I can't tell whether it's somehow guaranteed to work.
However, if I want to pass my own allocator, it seems to me that I'm out of luck: I'd expect that the zero-allocated memory will not work, but I can't really do void * = new std::vector<T, MyAllocator>(0, my_allocator) or similar, because I cannot know T. If I needed a vector of type std::vector<T *> instead, I'd assume it would be possible to just use a std::vecotr<void *> since the data is untyped in memory anyways.
Is there any way to allocate std::vector<T, MyAllocator> with unknown type T at compile time (cross-platform...)?
It's clearly not intended use nor clean code, but for the sake of the question, let's suppose I can't do anything about it.