Discussion:
Returning a ref to a temporary already possible?
Shriramana Sharma via Digitalmars-d-learn
2014-10-23 05:09:34 UTC
Permalink
IIUC the whole deal with not allowing ref parameters to point to
rvalues is the fear that they may be returned outside the function,
but it seems this is already possible:

module reftest ;
import std.stdio ;
struct Pair {
int x, y ;
this (int x_, int y_) { x = x_ ; y = y_ ; writeln("Pair constructed") ; }
auto handle() { return this ; }
}
void main() {
auto P = Pair(1, 2).handle ;
writeln(typeof(P).stringof) ;
P.x = 3 ;
writeln(P.x, ' ', P.y) ;
}

Running this makes it clear that only the one Pair object is ever
constructed. But I don't understand how it is that the type of P is
Pair and not ref(Pair).
--
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
H. S. Teoh via Digitalmars-d-learn
2014-10-23 05:24:40 UTC
Permalink
Post by Shriramana Sharma via Digitalmars-d-learn
IIUC the whole deal with not allowing ref parameters to point to
rvalues is the fear that they may be returned outside the function,
module reftest ;
import std.stdio ;
struct Pair {
int x, y ;
this (int x_, int y_) { x = x_ ; y = y_ ; writeln("Pair constructed") ; }
auto handle() { return this ; }
}
void main() {
auto P = Pair(1, 2).handle ;
writeln(typeof(P).stringof) ;
P.x = 3 ;
writeln(P.x, ' ', P.y) ;
}
Running this makes it clear that only the one Pair object is ever
constructed. But I don't understand how it is that the type of P is
Pair and not ref(Pair).
[...]

ref is a storage class, not a type constructor. There is actually no
such type as ref(Pair). The 'ref' applies only to the function
parameter (or return value), NOT to whatever it gets assigned to.


T
--
Microsoft is to operating systems & security ... what McDonalds is to gourmet cooking.
Shriramana Sharma via Digitalmars-d-learn
2014-10-23 11:25:52 UTC
Permalink
Hi thanks for the reply.

So should I understand that "return this" doesn't really return the
ref but the object actually? Or it does return a reference but since
it's assigned a name, it lives on under that name and so only the
single struct object P is ever created? Otherwise one would wonder how
a temporary can have lifetime beyond the line in which it is created.

I'd really like to know what's exactly happening in that line:

auto P = Pair(1, 2).handle ;

Thanks!


--
Shriramana Sharma ஶ்ரீரமணஶர
ketmar via Digitalmars-d-learn
2014-10-23 11:36:17 UTC
Permalink
On Thu, 23 Oct 2014 16:55:52 +0530
Shriramana Sharma via Digitalmars-d-learn
Post by Shriramana Sharma via Digitalmars-d-learn
auto P = Pair(1, 2).handle ;
object moving. add postblit (`this (this)`) and you'll see that.
ketmar via Digitalmars-d-learn
2014-10-23 11:38:22 UTC
Permalink
On Thu, 23 Oct 2014 14:36:17 +0300
Post by ketmar via Digitalmars-d-learn
On Thu, 23 Oct 2014 16:55:52 +0530
Shriramana Sharma via Digitalmars-d-learn
Post by Shriramana Sharma via Digitalmars-d-learn
auto P = Pair(1, 2).handle ;
object moving. add postblit (`this (this)`) and you'll see that.
or, to be precise, object *copying*. it's not the same as creating new
object. if you'll add destructor, you'll see that destructor is called
twice: once for local copy and once for P.

Continue reading on narkive:
Loading...