[ref variables] Allow `auto ref`

Timon Gehr timon.gehr at gmx.ch
Tue Jul 30 08:21:09 UTC 2024


On 7/30/24 07:43, IchorDev wrote:
> I’d also love it if `auto ref` worked inside all templates, not just 
> template functions. Often I want to do this:
> ```d
> struct S(T){
>    this(auto ref T x){}
> }
> ```
> But I’d get an error:
> ```d
> S!long x; //Error: cannot explicitly instantiate template function with 
> `auto ref` parameter
> ```
> If I make `this` a template function it works. For template functions, 
> even explicit instantiation works just fine. This requirement feels 
> rather silly and leads to a lot of unnecessary parameter-less templates.

Well, the `ref` and non-`ref` version of the function are two distinct 
instantiations of the function, while when instantiating `S` I would 
only expect to get one instance of the constructor. I guess your 
proposal would be that all combinations of ref-ness of the constructor 
parameters are automatically expanded as soon as `S` is instantiated? 
Why does this need a template at all?


More information about the Digitalmars-d mailing list