Tom Tom - 8 months ago 35
C++ Question

Is it accurate to say that C++ is a partially type safe language?

There are some features in C++ that are type safe and some other features that are not.

Example of C++ type safety:

char c = 'a';
int *p = &c; // this is not allowed (compiler error)

Example of C++ lack of type safety:

int *p; // I don't have to initialize p, and so it will have some junk/random value
*p = 12345; // this will probably lead to segmentation fault!

I have read in a couple of articles that C++ is not a type safe language. Can I consider it to be a non type safe language, or is it more accurate to say that it is a partially type safe language?


It's reasonable and practically meaningful to say that C++ is a partially type safe language.

C++ started as an extension of original mid- to late 1970's C, which was designed as a kind of high level portable assembly language, to make Unix more portable and easier to maintain. C++ added type safety for its new features, but with the goal of being mainly compatible with C (in particular using all those existing C libraries, including their headers) the original core of C had to be left as it was.

In particular, C++ got the decay of array to pointer from C. In C++ it isn't type safe because it allows an implicit conversion from array of Derived to pointer to Base, which can in turn be indexed but with Undefined Behavior.