www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct Postblit Void Initialization

reply Jiyan <jiyan jiyan.info> writes:
Hey,
just wanted to know whether something like this would be possible 
sowmehow:

struct S
{
int m;
int n;
this(this)
{
m = void;
n = n;
}
}

So not the whole struct is moved everytime f.e. a function is 
called, but only n has to be "filled"
Jul 30 2017
next sibling parent reply Eugene Wissner <belka caraus.de> writes:
On Sunday, 30 July 2017 at 19:22:07 UTC, Jiyan wrote:
 Hey,
 just wanted to know whether something like this would be 
 possible sowmehow:

 struct S
 {
 int m;
 int n;
 this(this)
 {
 m = void;
 n = n;
 }
 }

 So not the whole struct is moved everytime f.e. a function is 
 called, but only n has to be "filled"
this(this) is called after the struct is copied. Doing something in the postblit constructor is too late. The second thing is that the struct is copied with memcpy. What you propose would require 2 memcpy calls to copy the first part of the struct and then the second part. Besides it is difficult to implement, it may reduce the performance of the copying since memcpy is optimized to copy memory chunks.
Jul 30 2017
parent Jiyan <jiyan jiyan.info> writes:
On Sunday, 30 July 2017 at 19:32:48 UTC, Eugene Wissner wrote:
 On Sunday, 30 July 2017 at 19:22:07 UTC, Jiyan wrote:
 Hey,
 just wanted to know whether something like this would be 
 possible sowmehow:

 struct S
 {
 int m;
 int n;
 this(this)
 {
 m = void;
 n = n;
 }
 }

 So not the whole struct is moved everytime f.e. a function is 
 called, but only n has to be "filled"
this(this) is called after the struct is copied. Doing something in the postblit constructor is too late. The second thing is that the struct is copied with memcpy. What you propose would require 2 memcpy calls to copy the first part of the struct and then the second part. Besides it is difficult to implement, it may reduce the performance of the copying since memcpy is optimized to copy memory chunks.
Ok thank you :)
Jul 30 2017
prev sibling parent Moritz Maxeiner <moritz ucworks.org> writes:
On Sunday, 30 July 2017 at 19:22:07 UTC, Jiyan wrote:
 Hey,
 just wanted to know whether something like this would be 
 possible sowmehow:

 struct S
 {
 int m;
 int n;
 this(this)
 {
 m = void;
 n = n;
 }
 }

 So not the whole struct is moved everytime f.e. a function is 
 called, but only n has to be "filled"
I'll assume you mean copying (as per the title) not moving (because moving doesn't make sense to me in this context); use a dedicated method: struct S { int m, n; S sparseDup() { S obj; obj.n = n; return obj; } }
Jul 30 2017