Blitz logo

Blitz Support :

From: Julian Cummings (cummings_at_[hidden])
Date: 2003-10-14 13:26:54


Hi Patrik,

I think the issue here is very similar to the one that was raised
recently regarding the constructor argument type used to build a blitz
TinyVector. We use pass-by-value constructor arguments rather than
pass-by-const-reference because it is more efficient
for the most typical case, where the element type is a built-in numeric
type. In the same
way, it will usually be more efficient to return by value, unless the
element type happens
to be a large object that is costly to copy.

I can see how you were surprised by this behavior in the code you wrote.
 Nevertheless,
I think it makes the most sense to maintain the current semantics, since
this should be
the most efficient for the most common usage of TinyVectors.

Regards, Julian C.

P.S. I note that the Vector class in the tvmet package also uses this
return-by-value semantic.

Todd Veldhuizen wrote:

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

-- 
Dr. Julian C. Cummings                       E-mail: cummings_at_[hidden]
California Institute of Technology           Phone:  626-395-2543
1200 E. California Blvd., Mail Code 158-79   Fax:    626-584-5917
Pasadena, CA 91125