Bug 21317 – Copy constructor defined but blitting still occurs

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-10-16T07:28:48Z
Last change time
2020-10-20T03:13:55Z
Assigned to
No Owner
Creator
Eyal

Comments

Comment #0 by eyal — 2020-10-16T07:28:48Z
When defining a copy constructor, lack of NRVO (#21316) can still cause implicit blitting, despite the DIP[1] claiming: "When a copy constructor is defined for a struct, all implicit blitting is disabled for that struct". For example, this program (same as #21316 but with a copy constructor): import std.stdio; struct S { this(int x) { writefln("%s", &this); } ~this() { writefln("%s", &this); } this(scope ref return S) {} } unittest { auto f() { return S(1); } auto x = f(); } Still does blit/copy, silently. [1] https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1018.md
Comment #1 by razvan.nitu1305 — 2020-10-20T03:13:55Z
The copy constructor DIP specifies that the copy constructor may be called only in the case of lvalues; in the case of rvalues a move is performed implicitly. The cited paragraph: "When a copy constructor is defined for a struct, all implicit blitting is disabled for that struct" was referring only to lvalues because that is where you would normally call a copy constructor (although, I agree that the wording should have been better). In this situation the compiler constructs S(1) in the stack of f and then does a move. This is not optimal, but it has nothing to do with copy constructors. Since you already filed an enhancement request for this code, I am going to close this issue as it has nothing to do with copy constructors.