DIP 1016--ref T accepts r-values--Final Review

Nicholas Wilson iamthewilsonator at hotmail.com
Mon Nov 19 23:39:23 UTC 2018


On Monday, 19 November 2018 at 15:23:14 UTC, Steven Schveighoffer 
wrote:
> We can reduce to linear growth using auto ref:
>
> void lval_only()(int x, auto ref int y, auto ref int z) @disable
> void lval_only()(ref int, int, auto ref int) @disable
> void lval_only(ref int, ref int, int) @disable
>
> Still not ideal though. I tried auto ref for all 3, but that 
> means it will match both the template and the real function.
>
> I tried using a constraint, but not sure how to get the 
> parameter ref-ness in a neat way inside the constraint. This 
> does work:
>
> void lval_only()(auto ref int x, auto ref int y, auto ref int 
> z) @disable if (!__traits(isRef, x) || !__traits(isRef, y) || 
> !__traits(isRef, z))
>
> But it is a bit ugly.
>

template isRefX(A...) if (A.length == 1)
{
     enum bool isRefX = __traits(isRef, A[0]);
}
template allIsRef(A...)
{
     enum allIsRef = allSatisfy!(isRefX, A);
}

void lval_only()(auto ref int x, auto ref int y, auto ref int z)  
@disable if (!allIsRef!(x, y, z));

> Another option is to use a variadic template, verify the 
> parameters match the lvalue version, and then use some testing 
> on the parameter tuple.

Yuck.

> Or use a mixin based on the original function.
>
> Probably the mixin could work something like:
>
> mixin(disableNonRef!lval_only);

That doesn't look too bad.



More information about the Digitalmars-d mailing list