Problem with const correctness

bearophile bearophileHUGS at lycos.com
Fri Dec 7 07:14:21 PST 2012


Gor Gyolchanyan:

> I enforce value-type semantics by duplicating the arrays on 
> copy, so it
> should behave like a value type with no indirections 
> (implicitly convert to
> immutable).
> What do I need to do for this to work?

This is a first try, not tested much, and I don't know how many
copies it performs:


import std.traits;

struct Array(T) {
      this(T items[]...) {
          this._array = items;
      }

      this(this) {
          static if (isMutable!T)
              this._array = this._array.dup;
          else
              this._array = this._array.idup;
      }

      ref Array opAssign(in Array other) {
          static if (isMutable!T)
              this._array = other._array.dup;
          else
              this._array = other._array.idup;
          return this;
      }

      @property Array!(immutable(T)) idup() {
          return typeof(return)(this._array.idup);
      }

      private T[] _array;
}

void main() {
      auto one = Array!int(1, 2, 3, 4, 5);
      immutable two = one.idup;
}


Bye,
bearophile


More information about the Digitalmars-d mailing list