www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initialise non-copyable, non-default-constrauctable member struct

reply Peter Particle <ParticlePeter gmx.de> writes:
Question is related to this:
https://dlang.org/spec/struct.html#disable_default_construction

struct S {
     int x;
      disable this();     // Disables default construction
      disable this(this); // Disable copying

     this(int v) { x = v; }
}

struct T {
     float y;
     S s;
      disable this();     // Disables default construction
      disable this(this); // Disable copying

     this(float v) {
         y = v;

         s = S(cast(int)v);    // tried option 1
         s.__ctor(cast(int)v); // tried option 2

     }
}

Error: struct `S` is not copyable because it is annotated with 
` disable`
in both cases.

Is there some way around this problem?
Jul 27 2018
parent reply aliak <something something.com> writes:
On Friday, 27 July 2018 at 12:11:37 UTC, Peter Particle wrote:
 Question is related to this:
 https://dlang.org/spec/struct.html#disable_default_construction

 struct S {
     int x;
      disable this();     // Disables default construction
      disable this(this); // Disable copying

     this(int v) { x = v; }
 }

 struct T {
     float y;
     S s;
      disable this();     // Disables default construction
      disable this(this); // Disable copying

     this(float v) {
         y = v;

         s = S(cast(int)v);    // tried option 1
         s.__ctor(cast(int)v); // tried option 2

     }
 }

 Error: struct `S` is not copyable because it is annotated with 
 ` disable`
 in both cases.

 Is there some way around this problem?
This works on the latest dmd: struct S { int x; disable this(); // Disables default construction disable this(this); // Disable copying this(int v) { x = v; } } struct T { float y; S s; disable this(); // Disables default construction disable this(this); // Disable copying this(float v) { y = v; s = S(cast(int)v); } } void main() { auto s = T(3); } https://run.dlang.io/is/lLrUiq
Jul 27 2018
parent reply aliak <something something.com> writes:
On Friday, 27 July 2018 at 13:05:07 UTC, aliak wrote:
 https://run.dlang.io/is/lLrUiq
Sorry: https://run.dlang.io/is/20FUoj
Jul 27 2018
parent Peter Particle <ParticlePeter gmx.de> writes:
On Friday, 27 July 2018 at 13:06:10 UTC, aliak wrote:
 On Friday, 27 July 2018 at 13:05:07 UTC, aliak wrote:
 https://run.dlang.io/is/lLrUiq
Sorry: https://run.dlang.io/is/20FUoj
Thanks, you are right, but it seems that I have reduced my issue too much, as it still does not work with my actual case. I will try to properly reduce my code to present my issue. I think that the simple example can be fully figured out statically, but my case not.
Jul 27 2018