lvalue method
BCS
none at anon.com
Wed Oct 13 06:50:53 PDT 2010
Hello Benjamin,
> Am 08.10.2010 11:13, schrieb Lars T. Kyllingstad:
>
>> On Fri, 08 Oct 2010 09:33:22 +0200, Benjamin Thaut wrote:
>>
>>> Hi, I'm writing a vec4 math struct and I have a method of which the
>>> return value has to be a lvalue so I wonder which is the correct way
>>> to do this:
>>>
>>> vec4 Normalize() const { ... } //won't work, not a lvalue
>>>
>>> ref vec4 Normalize() const {
>>> vec4 temp;
>>> ...
>>> return temp;
>>> } //will this lead to a segfault or not?
>> The compiler shouldn't even accept this. When I try a similar thing,
>> DMD says "Error: escaping reference to local variable temp".
>>
>>> ref vec4 Normalize() const {
>>> vec4* temp = new vec4;
>>> ...
>>> return *temp;
>>> } //ugly, don't want to allocate anything on the heap
>> This would work, since the variable is no longer on the stack and
>> thus survives the return of the function.
>>
>>> auto ref vec4 Normalize() const {
>>> vec4 temp;
>>> ...
>>> return temp;
>>> } //will this lead to a segfault?
>> Well, that should compile, but it doesn't work the way you want.
>> 'auto ref' means that the function returns by ref if the return
>> expression is an lvalue *and it would not be a reference to a local
>> or a parameter*. So for this example, your function would return by
>> value, not by ref.
>>
>>> Or do I need to do it totaly in some other way?
>>>
>> Yes, you do. :) You are trying to create a variable on the stack,
>> and return it by reference. The problem is that when the function
>> returns, its stack frame (the memory occupied by the function's local
>> variables) is "released". At that point the variable doesn't exist
>> anymore, and any reference to it would be invalid.
>>
>> -Lars
>>
> All this was only to get it to return a lvalue. I need a lvalue to be
> able to do stuff like this.
>
> vec4 v1 = vec4(...);
> vec4 v2 = vec4(...);
> vec4 v3 = v1.Cross(v2.Normalize()).Normalize();
> Here it complained that v2.Normalize is not a lvalue, for whatever
> reason.
Does Cross take a non const ref? I wouldn't think it would need a mutable
vector. If it's not const, that's your problem. Figure out how to make it
const (you "should" be able to as long as no mutation is being done) and
this problem should go away.
Also, how the heck do you define cross product for 2 4D vectors? I know how
to do 2x3D and I can guess how to do 3x4D.
--
... <IXOYE><
More information about the Digitalmars-d-learn
mailing list