[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strong Typing, Dynamic Languages, What to do?



Shriram Krishnamurthi <sk@cs.brown.edu> writes:

> Oscar Fuentes wrote:
> 
> > This last paragraph confused me. In what sense C++'s type system
> > fails? The whole point around C++ type system is that it is verified
> > at compile time in a way that there is no need for run-time checks
> 
> Define a new type, A.
> 
> Define a vector of ten A's.
> 
> Make an instance of this vector.
> 
> C++'s type system says that all dereferences of this vector will give
> you objects of type A.
> 
> Dereference the eleventh element of this vector.
> 
> Do you get an A?

The C++ standard does not says that _any_ _pointer_ dereference will
return a _well_ _constructed_ instance. If you want to stay safe,
avoid "naked" pointers. (And yes, it is perfectly possible to do C++
using wrapped pointers, and not just barely dressed pointers like
std::vector)

Still, the case you mention does show a "problem" with std::vector (or
with traditional C style arrays, which are more or less the same for
the case we are discussing). It is not a problem with the
_language_. The same applies to most Standard C++ Library, where
performance criteria prevails over safety. If run-time performance is
not as critical to you as safety, use a safe library.

-- 
Oscar