Converting a void* to a std::string

You just need to dynamically allocate it (because it probably needs to outlive the scope you’re using it in), then cast it back and forth: // Cast a dynamically allocated string to ‘void*’. void *vp = static_cast<void*>(new std::string(“it’s easy to break stuff like this!”)); // Then, in the function that’s using the UserEvent: // Cast … Read more

c: size of void*

Only data pointers. void * can hold any data pointer, but not function pointers. Here is a C FAQ. void *’s are only guaranteed to hold object (i.e. data) pointers; it is not portable to convert a function pointer to type void *. (On some machines, function addresses can be very large, bigger than any … Read more

casting via void* instead of using reinterpret_cast [duplicate]

For types for which such cast is permitted (e.g. if T1 is a POD-type and T2 is unsigned char), the approach with static_cast is well-defined by the Standard. On the other hand, reinterpret_cast is entirely implementation-defined – the only guarantee that you get for it is that you can cast a pointer type to any … Read more

Is there a way to avoid implicit conversion to void*?

Is there any way to disable implicit conversion to void* for pointers to a certain class? No, you can’t prevent the implicit conversion, but you could wrap the API function(s) in proxy functions that checks types at compile time and approve/disapprove them there. Example: #include <iostream> #include <string> #include <type_traits> void api(void* p) { // … Read more