lvalue method
Simen kjaeraas
simen.kjaras at gmail.com
Fri Oct 8 02:09:55 PDT 2010
Benjamin Thaut <code at benjamin-thaut.de> 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?
Will simply not compile.
(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 will work.
> auto ref vec4 Normalize() const {
> vec4 temp;
> ...
> return temp;
> } //will this lead to a segfault?
The compiler will conclude that temp cannot be returned as ref, and
thus do a value return.
> Or do I need to do it totaly in some other way?
Don't know. Why does it have to be an lvalue?
--
Simen
More information about the Digitalmars-d-learn
mailing list