ref{bool} parameters

Quirin Schroll qs.il.paperinik at gmail.com
Wed Aug 7 07:33:32 UTC 2024


Issue: Function templates with `auto ref` parameters can’t be 
instantiated independently form a function call.

Technically, it makes sense, because the `ref` presence for `auto 
ref` parameters is determined by the value category of the 
argument. No argument → no value category → can’t determine `ref` 
presence. But that means, you can’t take the address of such a 
function template instance.

One way to solve this is to pass something to the template. In 
[this enhancement 
issue](https://issues.dlang.org/show_bug.cgi?id=24611) I drafted 
the idea of passing `ref`, essentially as a 1-bit data token. The 
issue with that idea is that it can only work with `auto ref T` 
where `T` is a template type parameter.

The idea of today is allowing `ref` to carry a boolean value to 
tell if it’s there:
```d
void f(ref{false} int x); // void f(int x)
void f(ref{true}  int x); // void f(ref int x)
```
That boolean can be inferred for templates:
```d
void f(bool isRef)(ref{isRef} int x) {}

int x;
f(0); // f!false(0);
f(x); // f!true(x);
auto fp0 =
```

For variadics, the function can use a `bool[]` parameter:
```d
void f(bool[] areRef, Ts...)(ref{areRef} Ts args)

int x;
f(0, x); // f!([false, true], int, int)(0, x);
```

Unfortunately, that can’t help with `auto ref`.


More information about the dip.ideas mailing list