STL like Vector,Pair, Map
James Dennett
jdennett at acm.org
Wed Nov 14 23:18:41 PST 2007
Tim Keating wrote:
> Bill Baxter Wrote:
>
>>> Why?
>> STL containers act like value types.
>>
>> std::vector<int> a,b;
>> ...// put some stuff in vector a
>> b = a; // now b has a copy of every element of a
>> a[4] = 9; //b[4] unchanged
>>
>>
>> In D currently to get that behavior have to do .dup:
>> int[] a,b;
>> b = a.dup;
>
> But . . . the reason the STL uses value types is to minimize
> the complexity inherent in determining who owns objects passed
> by reference. Doesn't that problem just _go away_ in a
>garbage-collected language?
Only one (trivial) aspect of it does. In the presence of
mutability, shared ownership is very different from value
semantics.
> Could you not build an interface-compatible set of STL containers
> that use reference semantics instead? Admittedly, that's a big
> caveat for porting, but I wonder how much code you would actually
> have to change . . .
Plenty, though admittedly many containers are *not* copied
so some code would port easily. The pain would be in finding
places where copy semantics were needed; the best way to do
that would probably be to artificially disable copying in the
C++ code and check where sharing would be viable.
-- james
More information about the Digitalmars-d
mailing list