Re: [eigen] operator overloading vs expression templates, and the need to return references

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


here vector8s is supposed to fit into a single SSE register so you
should simply return it by value, just like you would do with a float
or int or whatever scalar type.

gael.

On Fri, Sep 11, 2009 at 8:30 PM, Benoit Jacob <jacob.benoit.1@xxxxxxxxx> wrote:
> 2009/9/11 Rohit Garg <rpg.314@xxxxxxxxx>:
>> Back to my previous question, can somebody explain to me on how to
>> suppress those warnings in a meaningful way?
>
> You mean with your function returning a reference to a local temporary,
>
>
>  inline vector8s&  operator+(vector8s &other)
>       {
>       vector8s temp;
>       temp.data=_mm_add_epi16(data,other.data);
>       return temp;
>       }
>
> ?
>
> There's no good way to get rid of that warning, because there issue
> here is really serious! I've never heard of a way to return a
> reference to local variable in a function, I don't see how that could
> work. In C++1x, they are introducing rvalue references, and if I
> understand correctly that's exactly what you want. But your above code
> doesn't look safe at all to me: "temp" lives on the stack, when the
> function returns the stack pointer is decremented and the content of
> "temp" may then be overwritten. Unless there is something that I don't
> understand : that happens frequently too!
>
> In eigen and more generally with expression templates, the solution is
> to return by value, but return very lightweight objects (these are the
> "expression objects") instead of returning plain matrices.
>
> Benoit
>
>
>



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/