[C++-sig] Re: Support for Indexing and Iteration Safety

Brett Calcott brett.calcott at paradise.net.nz
Fri May 30 14:35:29 CEST 2003


>
> <snip my entire posting quoted>

oops, apologies for the noise.

>
> > 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.  -

Right -- but in my case, I'm writing that code as well, so I can be as
intrusive as I want (you did ask if it bothered *me*)

> 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.
>

It won't crash, but it is annoying. Okay, *really* annoying.

>
> Did I change your mind yet? ;->
>
If you could solve the above problem it would be great. But I can live
without it.

I realise I am in a somewhat unique position though. I am the producer
of the C++ lib, and the python lib - AND I am the only consumer. So I
can live happily with workarounds. If I was publishing my material for
general consumption, I might be thinking differently.


> <snip a duplicate copy of the entire foregoing text>
>
Ugh -- must learn decent newsreader and stop cutting and pasting.







More information about the Cplusplus-sig mailing list