I have a program that has to store functions as void (*) (void*). Creating functions with that signature leads to code duplication (usually the first line is to cast the void pointer to the correct type) and reduces type safety (the void pointer can be cast to anything, such as the wrong type), so I was wondering if I can take a function of type void (*)(T*), and then convert it to a void(*)(void*) and call it like that, in a way similar to this:
#include <iostream>
#include <string>
void printer(std::string* string)
{
std::cout << *string << std::endl;
}
int main()
{
//Cast the function to a one that takes a void pointer
auto func = reinterpret_cast<void(*)(void*)>(&printer);
//Create a string and call the function with it
std::string string = "Hello World";
func(&string);
return 0;
}
The above code compiles and runs fine (on ideone) but I was wondering if it's at all standards compliant or if it's undefined behaviour and is just working correctly for my specific example and OS
[&print](void* value) {...}), but capture makes it "stateful" and now it can't be converted to function ptr.printerhere is a namespace member and not a function-local variable, lambda capture doesn't apply to it.