Why is void* considered unsafe in C++?
Because void*
represents a memory address without any type information. The compiler cannot know what data type structures are used what the raw memory contains at that address.
In that case the programmer is in charge to do the deciphering of the memory layout themselves correctly, which is an error prone process, and the programmer need to know exactly what they are doing there.
In that sense as it is said what you've been citing (emphasis mine)
- avoid
void*
(keep them inside low-level functions and data structures if you really need them and present type safe interfaces, usually templates, to your users)
it is about loosing type safety with void*
.
To add up about the cite above:
A template in c++ is preferred, because the original type information won't ever get lost as with generic c style functions using void*
as parameter.