www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opOpAssign

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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?
Jul 08 2016
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
auto opOpAssign(string op)(ptrdiff_t i) if(op =="+" || op == "-")
Jul 08 2016
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Friday, 8 July 2016 at 07:15:06 UTC, ketmar wrote:
 auto opOpAssign(string op)(ptrdiff_t i) if(op =="+" || op == 
 "-")
Derp. Thanks!
Jul 08 2016