Chris Cain wrote:
> On Thursday, 3 May 2012 at 10:03:55 UTC, Jens Mueller wrote:
> >What is a good solution when using member functions on a
> >range's/container's element?
> >
> >Note, the problem only applies when storing structs because
> >classes
> >behave like references.
> 
> I think in this case, it might make sense to store pointers to
> structs.
> 
> So,
> 
> auto c = R!(Point*)(10);
> 
> OR
> 
> alias Point* PointRef;
> auto c = R!PointRef(10);
> 
> 
> Let me know if there's something wrong with this approach.

Assume you are using Phobos which returns a range as ranges are the
glue keeping Phobos together and leading to a coherent interface. Then
there is no way to get a pointer or similar. Your solution does work in
cases when I control the storage but that should be rare cases.
Additionally using pointers clutters the code I think.

In essence: Whenever you are using a range of some struct in Phobos then
calling a mutating member function on its elements won't do what you are
expecting. You have to resort to copying/moving. I.e. a range's element
can only be changed by copying/moving.

I wonder how often other Phobos users run into this issue. Only Peter
finds this issue to be problematic. When we have ranges everywhere the
result may be much more troublesome.

Thanks for your reply.

Jens

Reply via email to