Looking for documentation of D's lower-level aspects.

Jonathan M Davis jmdavisProg at gmx.com
Sat Oct 22 21:04:45 PDT 2011


On Sunday, October 23, 2011 03:25:48 Sean Silva wrote:
> == Quote from Jonathan M Davis (jmdavisProg at gmx.com)'s article
> 
> > On Sunday, October 23, 2011 00:01:42 Sean Silva wrote:
> > They're all supposed to be reference types.
> 
> What prompted the decision for that? Doesn't that incur an extra heap
> allocation for the containers, and an extra level of indirection? I mean,
> with value-semantics like STL containers, you can use it like a value, e.g.
> as a local in a function, and have no overhead, but if you want to wrap a
> class around it and have it by reference, then you can, and it is no less
> efficient than if the containers were written that way. But if the
> containers are already by reference, you can't return them to having value
> semantics without adding even more indirection and inefficiency.

I'd have to go digging through the newsgroup archives to give all of the 
reasons but it basically comes down to the fact that people very rarely want 
to pass containers by value, and the fact that C++ makes it so that the 
default behavior of passing container is to copy them is a frequent source of 
bugs. In C++, containers are almost always passed by reference with & or * 
rather than passing them by value, and if you forget to make the function take 
or return via reference or pointer, it's a major performance hit. So IIRC, 
Andrei thought that the fact that C++ containers are value types was a big 
mistake of C++.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list