struct Pointer(uint p, T) if(p <= Generic)
{
    T* ptr;
    ref T opUnary(string op)() if(op=="*")
    {
        return *ptr;
    }
    ref T opIndex(size_t i)
    {
        return *(ptr+i);
    }
auto opBinary(string op)(ptrdiff_t i) if(op == "+" || op == "-")
    {
        return Pointer!(p, T) (mixin("ptr" ~ op ~ "i"));
    }

auto opOpAssign(string op)(ptrdiff_t i) if(op =="+=" || op == "-=")
    {
        //mixin("ptr"~op~"i;");
        ptr += i;
    }
}

void main()
{
    int a = void;
    alias fooint = Pointer!(2,int);
    auto b = fooint(&a);
    auto c = *b;
    *b = 42;
    b[0] = 7;
    auto e = b + 5;
    auto f = b - 4;
    auto g = &a;
    g += 3;
    b += 7;
    b -= 9;
}

errors with
pointer.d(23): Error: 'b += 7' is not a scalar, it is a Pointer!(2u, int) pointer.d(24): Error: 'b -= 9' is not a scalar, it is a Pointer!(2u, int) irrespective of the presence or absence of the opOpAssign method or its definition.

surely this should work. What am I doing wrong?
  • opOpAssign Nicholas Wilson via Digitalmars-d-learn

Reply via email to