Array type conversion

Mark Burnett unstained at gmail.com
Sat Apr 28 12:10:09 PDT 2007


Manfred Nowak Wrote:

> Mark Burnett wrote
> 
> > only one of them isn't an Apple and you have undefined behavior. 
> 
> From the specs:
> | Multiple dynamic arrays can share all or parts of the array data.
> 
> With the asssignment:
>   justsomefruits = lotsofapples;
> you did pointer assignments and thereby declared that the array data 
> can be interpreted as both: Fruits and Apples. The handling of this 
> declaration is up to your intelligence. If you fail, then you might 
> have tricked out yourself.
> 
> If you wanted a componentwise array copy you should have written:
>   justsomefruits[] = lotsofapples; // observe the []
> and the compiler would have served you with appropriate error messages.
> 
> -manfred

Certainly making a copy prevents this problem, but what I'm really curious about is the motivation for including these imlicit conversions (they are mentioned specifically on the Array description page on digitalmars.com).

They're not safe in general, so I imainge library designers will end up using their own user-defined array/vector type just as in c++, so that such errors are caught at compile time.  This seems to limit the usefulness of these built in array types.  Sure they're bounds-safe, but they don't seem 100% type-safe.

For example:

std::vector<derived> ad;
std::vector<base> ab;

ab = ad; // c++ compiler error

A compiler error is more what I would expect.  Treating a container of derived as a container of base is an error.

Not to speculate too much on the reasons, but is it just too much overhead for a built-in type to disallow this behavior?  Am I underestimating the usefulness of these built-in arrays in library design?

Mark

PS Thanks for your replies so far ;)



More information about the Digitalmars-d mailing list