Bypassing the postblit?

John Colvin john.loughran.colvin at gmail.com
Thu Dec 26 00:56:51 PST 2013


On Thursday, 26 December 2013 at 05:06:26 UTC, Ritu wrote:
> Dear D Experts
>
> I have a situation where I want to pass a D struct object as an 
> argument to a function. Now in my case, I do not want to invoke 
> the default D copy constructor for the struct. Is there a way 
> of doing that? I have tried disabling the postblit and enabling 
> another constructor via "alias this" (see the code below). But 
> the compiler does not like that, and I get an error that tells 
> me that the Bar is not copyable since it has been disabled:
>
> struct Bar {
>   @disable this(this);
>   alias _foo this;
>   Foo _foo;
>   public this(Foo foo) {
>     this._foo = foo;
>   }
> }
>
> class Foo {}
>
> void main() {
>   Bar bar;
>   Bar baz = bar;
> }
>
> Regards
> Ritu

D struct copying is done by a bit-level copy of the source. A 
postblit - the closest we have to a struct copy constructor - is 
only run if you specify one yourself, i.e. there is no default 
one to disable.


More information about the Digitalmars-d mailing list