www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Very fortunately `auto ref` prevents overloading

reply user1234 <user1234 12.de> writes:
Very fortunately `auto ref` prevents overloading. After a post on 
HN that get very little interest[0] I've found that the D 
language is able to monomorphize a function template in a way 
that's very unexpected, or at least unspecified:

```d
auto ref T max(T)(auto ref T u, auto ref T v) => u > v ? u : v;

void main()
{
     int a = 1;
     int b = 0;
     int x;

     // lvalue version
     assert(&max(a,b) is &a);
     // rvalue version
     assert(max(a,0) == a);
}
```

so at some point you have to imagine me trying this code, because 
"it's not sure if that will work".

Not very interesting you would say. Indeed when a problem is 
resolved you just dont even think it can exist.

[0]: https://simont.dreamwidth.org/242479.html
Oct 21
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Wednesday, 22 October 2025 at 05:23:27 UTC, user1234 wrote:
 or at least unspecified:
what? https://dlang.org/spec/function.html#auto-ref-functions https://dlang.org/spec/template.html#auto-ref-parameters The one of the examples is a max function.
Oct 21
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
I'm curious about the HN reference?
Oct 23
parent Paul Backus <snarwin gmail.com> writes:
On Thursday, 23 October 2025 at 18:50:46 UTC, Walter Bright wrote:
 I'm curious about the HN reference?
Looks like this is the thread: https://news.ycombinator.com/item?id=45626559
Oct 23