Deprecating this(this)

Shachar Shemesh shachar at weka.io
Mon Apr 2 08:56:17 UTC 2018


By sheer coincidence, I've just stumbled upon another limitation of 
this(this). I am not sure whether it is already documented.

Let's define struct S1 with no copying allowed, and put it as a member 
of struct S2. Under C++ as well as under D, this automatically makes S2 
also non-copyable.
Under C++, however, I can do this:

struct S1 {
     S1() {}

     // Make S1 non-copyable
     S1(const S1 &that) = delete;
     S1 &operator=(const S1 &that) = delete;
};

struct S2 {
     int a;
     S1 s;

     S2(int _a) : a(_a) {
     }

     S2(const S2 &that) : a(that.a) {
     }
};

int main() {
     S2 s(17);
     S2 v(s); // This compiles, invoking S2's copy ctor
}


In other words, I can tell the compiler that I know how to copy S2 
without having to copy the member S1.

Under D, this simply doesn't work. If S1 has @disable this(this), any 
struct that has S1 as a member will be uncopyable, and this is not 
overridable.

Shachar


More information about the Digitalmars-d mailing list