[C++-sig] Re: Support for Indexing and Iteration Safety
David Abrahams
dave at boost-consulting.com
Thu May 29 15:42:00 CEST 2003
"Brett Calcott" <brett.calcott at paradise.net.nz> writes:
<snip my entire posting quoted>
> I'm haven't gone far enough in what I am doing to encounter these
> problems, but for what its worth, at the moment I would have done the
> following:
>
> if something is lightweight, return a copy
> if it is big, use shared_ptr
>
> Which means that the above problems 3 & 4 would not occur.
That is only true if you can modify your vector to be a
vector<shared_ptr<SomeBigObject> >. Often, the implementation of code
being wrapped can't be intrusively changed in that way. Furthermore,
even if it's lightweight, returning a copy gets you unpredictable
behavior like:
>>> v[3].foo = 1
>>> v[3].foo
22
because you've only set the attribute on the copy.
> And 1 & 2 don't really seem that onerous a task. So I guess I don't
> 'care' that much.
Hmm, this might be a case of "those sophisticated enough to know what
I'm talking about also know how to avoid the problems."
Where are all the other respondents? I know a number of people have
tried to wrap things with array indexing in the past...
http://aspn.activestate.com/ASPN/Mail/Message/1428806
http://aspn.activestate.com/ASPN/Mail/Message/1465837
http://aspn.activestate.com/ASPN/Mail/Message/1227141
http://aspn.activestate.com/ASPN/Mail/Message/1227504
http://aspn.activestate.com/ASPN/Mail/Message/1431504
http://aspn.activestate.com/ASPN/Mail/Message/1425273
http://aspn.activestate.com/ASPN/Mail/Message/1227378
> Of course, I reserve the right to change my opinion at any moment if I
> encounter problems :)
Did I change your mind yet? ;->
<snip a duplicate copy of the entire foregoing text>
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list