Rebooting the __metada/__mutable discussion

Alexandru Ermicioi alexandru.ermicioi at gmail.com
Mon Apr 11 16:40:42 UTC 2022


On Monday, 11 April 2022 at 14:44:18 UTC, Paul Backus wrote:
> There's no issue here. You just write the copy constructor of 
> RC!T to require a mutable source and destination object, and 
> the compiler will refuse to copy a const(RC!T). (You can still 
> pass by `const ref`, of course.)

Obviously there are:
```d
import std;

void main()
{
     int j = 2;
     int k = 3;
     const r3 = Counted!int(&j);
     const(Counted!int) r4 = Counted!int(&k);
}

@safe struct Counted(T)
{
     private T* subject;
     private size_t* counter;

     this(T* subject)
     {
         this.counter = new size_t;
         this.subject = subject;
         *this.counter = 1;
     }

     this(ref T subject) const @disable;
     this(T* subject) const @disable;

     this(ref Counted!T counted)
     {
         this.subject = counted.subject;
         this.counter = counted.counter;
         ++(*this.counter);
     }

     this(ref Counted!(const T) counted) const @disable;
     this(ref const Counted!T counted) const @disable;
     this(ref Counted!T counted) const @disable;

     this(this) @disable;

     ~this()
     {
         (*this.counter)--;

         if ((*this.counter) == 0)
         {
             writeln("Freeing the subject: ", *this.subject);

             this.counter = null;
         }
     }

     // ~this() const @disable; Obviously another bug or uncharted 
area.

     void opAssign(ref Counted!T counted)
     {
         --(*this.counter);
         this.subject = counted.subject;
         this.counter = counted.counter;
         ++(*this.counter);
     }

     void opAssign(Counted!T counted)
     {
         this = counted;
     }

     void opAssign(ref const(Counted!T) counted) const @disable;
     void opAssign(const(Counted!T) counted) const @disable;
     void opAssign(Counted!(const T) counted) const @disable;
     void opAssign(ref Counted!(const T) counted) const @disable;
     void opAssign(ref Counted!T counted) const @disable;
     void opAssign(Counted!T counted) const @disable;

     void opCast(T)() @disable;
}
```

Or I missed something that will prevent these two use cases.




More information about the Digitalmars-d mailing list