www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why postblit cannot be disabled when copy ctor is defined

reply Fanda Vacek <fanda.vacek gmail.com> writes:
import std.stdio;

struct A
{
	int x;
	this(ref return scope A rhs)
	{
		writeln("copy ctor: ", x);
	}
	 disable this(this) {writeln("postblit: ", x); }
}

void main()
{
	A a;
	A b = a; // copy constructor gets called
}

I've got error: Error: struct `tst.A` is not copyable because it 
is annotated with ` disable`

but it should not be used according to 
https://dlang.org/spec/struct.html#struct-postblit

WARNING: The postblit is considered legacy and is not recommended 
for new code. Code should use copy constructors defined in the 
previous section. For backward compatibility reasons, a struct 
that defines both a copy constructor and a postblit will only use 
the postblit for implicit copying.
Nov 07
next sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
On Thu, Nov 7, 2019 at 6:30 PM Fanda Vacek via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
 import std.stdio;

 struct A
 {
         int x;
         this(ref return scope A rhs)
         {
                 writeln("copy ctor: ", x);
         }
          disable this(this) {writeln("postblit: ", x); }
 }

 void main()
 {
         A a;
         A b = a; // copy constructor gets called
 }

 I've got error: Error: struct `tst.A` is not copyable because it
 is annotated with ` disable`

 but it should not be used according to
 https://dlang.org/spec/struct.html#struct-postblit

 WARNING: The postblit is considered legacy and is not recommended
 for new code. Code should use copy constructors defined in the
 previous section. For backward compatibility reasons, a struct
 that defines both a copy constructor and a postblit will only use
 the postblit for implicit copying.
This is a bug, please fill an issue on https://issues.dlang.org/
Nov 07
parent Fanda Vacek <fanda.vacek gmail.com> writes:
On Thursday, 7 November 2019 at 19:10:50 UTC, Daniel Kozak wrote:
 On Thu, Nov 7, 2019 at 6:30 PM Fanda Vacek via Digitalmars-d 
 <digitalmars-d puremagic.com> wrote:
 [...]
This is a bug, please fill an issue on https://issues.dlang.org/
issue created: https://issues.dlang.org/show_bug.cgi?id=20367 thanks Fanda
Nov 08
prev sibling parent RazvanN <razvan.nitu1305 gmail.com> writes:
On Thursday, 7 November 2019 at 17:28:44 UTC, Fanda Vacek wrote:
 import std.stdio;

 struct A
 {
 	int x;
 	this(ref return scope A rhs)
 	{
 		writeln("copy ctor: ", x);
 	}
 	 disable this(this) {writeln("postblit: ", x); }
 }

 void main()
 {
 	A a;
 	A b = a; // copy constructor gets called
 }

 I've got error: Error: struct `tst.A` is not copyable because 
 it is annotated with ` disable`

 but it should not be used according to 
 https://dlang.org/spec/struct.html#struct-postblit

 WARNING: The postblit is considered legacy and is not 
 recommended for new code. Code should use copy constructors 
 defined in the previous section. For backward compatibility 
 reasons, a struct that defines both a copy constructor and a 
 postblit will only use the postblit for implicit copying.
Once the postblit is defined (even if it is disabled), the copy constructor will be ignored. The solution to this is to simply comment out the postblit. If a library object has a postblit, then your hands are pretty much tied. This, indeed, could be fixed by considering that if you have a disabled postblit and a copy constructor, the copy constructor is preferred.
Nov 08