[Issue 22241] Compiler fails to match a more specialized overload

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jul 11 09:49:28 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=22241

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to Max Samukha from comment #0)

> But, if another general parameter is added, the "matches both" error occurs:
> 
> int foo(T)(T t, int a)
> {
>     return 1;
> }
> 
> int foo(T, U)(T t, U a)
> {
>     return 2;
> }
> 
> void main()
> {
>     int x;
>     assert(foo(0, x) == 1);
>     assert(foo(0, "a") == 2);
> }
> 
> onlineapp.d(14): Error: `onlineapp.foo` called with argument types `(int,
> int)` matches both...
> 
> Both should be either rejected or accepted.

In the first example, when `foo(x)` is encountered, the compiler selects the
first overload because it does not have to deduce any types for it
(`foo()(int)` is more specialized than `foo(T)(T a)`). In the second example,
when `foo(0, x)` is encountered the compiler needs to do deduction for both
overloads and after deduction it sees that both match equally => error. For
foo(0, "a") there is no ambiguity since the first overload does not match while
the second one does.

I can see an enhancement being made by counting the number of parameters for
which deduction is necessary and choosing the overload with fewer deduced
parameters, however, this gets nasty when combined with template constraints
and other template magic. I don't think this enhancement is worth it.

This is intended behavior, so I would vote to close this as WONTFIX.

What do you think?

--


More information about the Digitalmars-d-bugs mailing list