Annoying thing about auto ref function template

Yuxuan Shui via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 20 14:34:14 PDT 2017


On Monday, 20 March 2017 at 21:08:40 UTC, Jonathan M Davis wrote:
> On Monday, March 20, 2017 13:20:52 Jonathan M Davis via 
> Digitalmars-d wrote:
>> So, yes, this particular restriction can be annoying, but 
>> there is a good reason for the restriction (though the error 
>> message _is_ pretty bad), and I have no idea how we would fix 
>> the problem.
>
> After thinking about this further, it does occur to me that 
> there's a fairly simple workaround. e.g.
>
> auto foo(T)(T t)
> {
>     return bar(t);
> }
>
> auto bar(T)(auto ref T t)
> {
>     ...
> }
>
> Then you can just use foo!int. Now, that requires you to pick 
> whether it's ref or not, but if there were a way to explicitly 
> instantiate a function with an auto ref parameter, you'd have 
> to do that anyway.
>
> - Jonathan M Davis

This is a bit tedious because it requires you creating a new 
function.

Maybe we can create a template for that. But still, auto ref 
requires us to do things differently, which is annoying.


More information about the Digitalmars-d mailing list