Monday, 23 April 2018

Why does c++ forbid implicit conversion of void*?

You can do the conversion in C++, but it requires a cast. C++ aims to be a more type-safe language than C, so it tries to close some "holes in the type system" that C allows.




In C, this will be accepted without any diagnostics being required:



int x;
void *p = &x;
double *q = p;
*q = 0.0;


Type safety has been violated without any casts being present in the source code.




This is a C++ FAQ answered by B. Stroustrup, inventor of C++.



C++ doesn't ban the conversion; it just wants it to be documented by some kind of blurb in the source code, namely a cast which at least suggests, if not proves, that it's being done on purpose.



About the origin of void *, Stroustrup writes this (bolding mine):




Late in its history, C with Classes* began to support the notion of a pointer to ‘‘raw memory, void *. The origin of void * is shrouded in some mystery. I vaguely remember inventing it together with Larry
Rosler and Steve Johnson.
However, Dave Prosser remembers first having suggested it based on something
used "somewhere in Australia." Possibly both versions are correct because Dave worked closely with

Larry at the time. In either case, void * was introduced into both languages more or less at the same time.
The earliest mention of void * that I can find is in a memo dated January 1, 1983, about the memory management mechanisms provided by my C++ compiler, Cfront, so the origins of void * in C++ must go back at
least to mid-1982. The earliest written record of void * in the context of ANSI C is a proposal by Mike Meissner dated "12 Oct 83," which presented void *essentially as it was accepted into ANSI C in June 1984 [Prosser,2001]




So C++ inherits the void * concept as Stroustrup originally envisioned it, while around the same time, the C people had a slightly different idea that was more loose in type safety.



In the case of malloc, of course there are dangers; but those are already flagged by the presence of the well-known identifier malloc; the cast doesn't bring in anything more, but not requiring it specifically for malloc, while requiring it everywhere else would be an awkward exception to the rules.







* "C with Classes" is the predecessor to C++

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...