Is this undefined behavior that just happens to work on my implementation?
Yes. Most compilers implement a reference using a pointer under the hood, but that is not dictated by the standard.
Standard syntax does not allow a pointer to be assigned as-is to a reference, and vice versa, hence the compiler errors on those statements.
Casting everything to
void*, all bets are off on type safety. Using a C-style type cast, you can get away with just about anything. You are basically telling the compiler that you know what you are doing, so it should just accept it and be quiet. That is why such type-casts can be dangerous when you don’t know what you are really doing, and should be avoided unless absolutely necessary. If you must cast, prefer C++-style type casts instead (
reinterpret_cast, etc). At least then, you won’t give up quite as much type safety.
CLICK HERE to find out more related problems solutions.