"Type safety" means that the compiler checks that you are doing the right things with the right types (e.g triggers a compiler error if you attempt to treat a Banana as an Orange, or give a string to a function expecting to output an integer).
Type safety (mostly) goes right out of the window when void*
comes into the picture - it is a pointer that can point to anything (completely unaware of the types involved), and the language leaves going about with it completely in the programmers hands (for example, a void*
isn't mostly good for anything except for being cast back to the original type; it can represent anything, but you have to know what it is before you can use it).
Type unsafety also comes to play with variadic functions like printf (the compiler doesn't care how many arguments there are and what their types are - again it is up to the caller to make sure that the format string matches the arguments and their types).
Type-safe alternative to memcpy (for arrays and containers) could be std::copy
in
- it may be implemented in terms of memmove if all involved types satisfy certain requirements, otherwise it performs assignments - with some classes you can break certain invariants if you bypass their public interface and just go and move / copy them around in memory (for example, I suppose any class with a non-trivial copy constructor is going to misbehave if you make copies of it with memcpy).
Type-safe alternative to C I/O routines are iostreams (and if you want the benefits of the format string, boost::format
).