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 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:
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:
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:
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
.