![]() |
Blitz Support : |
From: Patrik (patrik_at_[hidden])
Date: 2003-10-11 17:20:49
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