DIP 1018--The Copy Constructor--Community Review Round 1
RazvanN
razvan.nitu1305 at gmail.com
Wed Jan 9 15:51:27 UTC 2019
On Wednesday, 9 January 2019 at 14:25:44 UTC, Atila Neves wrote:
> I didn't either until the example in the DIP:
>
> -----------
> struct C { this(ref C) {} }
> void fun(C c) {}
> void main() {
> C c;
> fun(c);
> }
> -----------
>
> c is moved before the DIP, and copied afterwards due to the
> existing constructor being considered a copy constructor.
C is not moved in this situation. The following code:
struct C { this(this) { import std.stdio; writeln("postblit"); } }
void fun(C c) {}
void main()
{
C c;
fun(c);
}
prints "postblit". Am I missing something?
> I wasn't referring to opMove, no, but I think adding a `this(C
> c)` constructor to the example would do it for rvalues?
The copy constructor is called solely on lvalues. `this(C c)` is
a simple constructor that needs to be called explicitly.
More information about the Digitalmars-d
mailing list