compile time 'address'

Dominic Jones dominic.jones at gmx.co.uk
Fri Nov 30 11:07:55 UTC 2018


On Friday, 30 November 2018 at 00:23:08 UTC, aliak wrote:


> I think you can do that by using aliases. This seems to work:
>
> auto cmp(alias t, alias u)() {
>   return &t == &u;
> }
>
> void main() {
>   auto c0 = 1;
>   auto c1 = 2;
>   static assert(cmp!(c0, c0));
>   static assert(!cmp!(c0, c1));
> }

Unfortunately, with the solution above, the variables are passed 
as template arguments. Somehow, I need to have the variables 
being passed as regular arguments since my intended use case is 
operator overloading for expression trees.


Consider the example, below: somehow, I need to be able to know 
the nominal addresses of parameters passed into 'opBinary' so 
that the 'Binary' template may be defined with address tags which 
relate to the operands of 'opBinary'. If the types and tags of 
the two variables match then there may be a guarantee (at the 
file scope) that the arguments are indeed the same.

struct Terminal
{
   auto opBinary(string op, R)(const ref R r)
   {
      enum l_adr = 0; // proposed: this.scope.offsetof + 
this.offsetof;
      enum r_adr = 4; // proposed: r.scope.offsetof + r.offsetof;

      return Binary!(op, typeof(this), R, l_adr, r_adr)(this, r);
   }
}

struct Binary(string op, L, R, int l_adr, int r_adr)
{
   L l;
   R r;
}

void main()
{
   Terminal c0;
   Terminal c1;
   pragma(msg, typeof(c0 + c1)); // 'Binary!("+", Terminal, 
Terminal, 0, 4)'

   // pragma(msg, typeof(transform(c0 + c0))); // becomes:  "2 * 
c0"
   // pragma(msg, typeof(transform(c0 + c1))); // becomes: "c0 + 
c1"
}

(at: https://run.dlang.io/is/hwVI5n)


-Dominic


More information about the Digitalmars-d mailing list