Is the address-of operator (&) really needed?

Artur Skawina art.08.09 at gmail.com
Fri Jun 1 12:48:01 PDT 2012


On 06/01/12 21:18, Jonathan M Davis wrote:
>>> 3. ref doesn't work with variadic templates very well. Take a
>>> look a
>>> std.getopt.getopt. It takes pointers, not refs, and there isn't
>>> a way to make
>>> it take refs.
>>
>> Is it because getopt() is a C function? If it is see my reply to
>> your point #1. I'll admit I do not know enough D to understand
>> what you are saying, some explanation will be helpful.
> 
> It's not a C function. It's a variadic template. It's instantiated with 
> whatever types it's given. It's literally _impossible_ to use ref with that 
> sort of function. So, if you want it to take the variable and write to it, you 
> have to pass a pointer to it.

   import std.stdio;

   void go(A...)(auto ref A a) {
      a[0] = 42;
      a[1].y--;
   }

   struct S { int y; }

   void main() {
     int x;
     S s;

     go(x, s);

     writeln(x, " ",  s);
   }


artur


More information about the Digitalmars-d mailing list