digitalmars.D.bugs - [Issue 10146] New: ref on return is ignored
- d-bugmail puremagic.com (67/67) May 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10146
- d-bugmail puremagic.com (16/16) May 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10146
- d-bugmail puremagic.com (44/44) May 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10146
- d-bugmail puremagic.com (9/11) May 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10146
- d-bugmail puremagic.com (6/6) May 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10146
http://d.puremagic.com/issues/show_bug.cgi?id=10146 Summary: ref on return is ignored Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: major Priority: P2 Component: DMD AssignedTo: nobody puremagic.com ReportedBy: rswhite4 googlemail.com The 'ref' by 'getA' is ignored. [code] import std.stdio; struct A { public: int id; this(int id) { writeln("CTor ", id); this.id = id; } this(this) { writeln("Postblit ", this.id); this.id *= 2; } ~this() { writeln("DTor ", this.id); } } class B { private: A _a; public: this() { this._a = A(42); } ref A getA() { writeln("Return A"); return this._a; } } void main() { B b = new B(); A a = b.getA(); } [/code] Expected Output: ---- CTor 42 DTor 0 Return A DTor 42 ---- Current Output: ---- CTor 42 DTor 0 Return A Postblit 42 DTor 84 DTor 42 ---- -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 23 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10146 Maxim Fomin <maxim maxim-fomin.ru> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED CC| |maxim maxim-fomin.ru Resolution| |INVALID Severity|major |normal --- D does not have references like C++. Each time you assign return value to a struct variable, refness is wiped out. The only cases when returning by ref matter, is when returned value is also returned by caller by ref or casted to pointer. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 23 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10146 I expected that in this case opAssign is called, not the postblit, beause I return by ref and assign then the ref to a new value. But if you change A to this: [code] struct A { public: int id; this(int id) { writeln("CTor ", id); this.id = id; } this(this) { writeln("Postblit ", this.id); this.id *= 2; } void opAssign(ref const A a) { writeln("opAssign L: ", a.id); this.id = a.id; } void opAssign(const A a) { writeln("opAssign R ", a.id); memcpy(&this, &a, A.sizeof); } ~this() { writeln("DTor ", this.id); } } [/code] You see still the same output, no call of opAssign by 'getA': ---- CTor 42 opAssign R 42 DTor 42 Return A Postblit 42 DTor 84 DTor 42 ---- Or did I miss something important here? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 23 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10146 ---I expected that in this case opAssign is called, not the postblit, beause I return by ref and assign then the ref to a new value.Actually no, because refness here does not matter. You return by ref, but get not a ref in the caller. You don't assign ref to a new value either. You have A a = Initializer; which calls copy constructor. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 23 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10146 That is absolute strange, but thanks for the explanation. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 23 2013