Blitz logo

Blitz Support :

From: Todd Veldhuizen (tveldhui_at_[hidden])
Date: 2003-10-11 17:46:45


I don't see any problem with having operator[] const
return const T& off the top of my head.. although I suppose
there might be some optimization issue there if the compiler
is not so smart. I'm kind of fuzzy on the difference between
(say) int and const int& when it comes to optimization.

On Sat, 11 Oct 2003, Patrik wrote:

> Hi everyone,
>
> I was just burned by what seems to me a syntactic peculiarity of the tiny
> vector class: operator [] returns a reference but operator [] const returns
> by value.
>
> If you use tiny vectors as numeric vectors, the way they are used in the
> Array class, for example, this may not matter. However, if you, like me,
> is using it as a convenient fixed-length, bounds-checked-when-necessary
> general vector, this means that you return a reference to the object as
> long as the tiny vector is not const, but if it is you return a temporary
> copy. Is this really the way you want it to work?
>
> The reason I discovered this was because my type was TinyVector < vector <
> double >, 4 >, and what happened was that when I did theVectors [2].begin
> () it would work as long as the tiny vector was not const, but when it was
> you return an iterator to a temporary vector -> hard-to-find-disaster ensues...
>
> Wouldn't it be more predictable to have operator [] const return const
> T_numtype&? Are there performance issues? If we want to keep the design
> as it is, I would at least suggest putting a large caveat in the
> documentation pointing out the fact that you will return temporary copies
> of (possibly large) objects if the template type is not a built-in.
>
> /Patrik
>
> _______________________________________________
> Blitz-support mailing list
> Blitz-support_at_[hidden]
> http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>

-- 
Todd Veldhuizen  /  tveldhui_at_[hidden]  /  Indiana University Computer Science