Thread overview
Picking function templates with __traits(getOverloads, ..., true)
Jul 29, 2020
Jean-Louis Leroy
Jul 30, 2020
user1234
Jul 30, 2020
Jean-Louis Leroy
July 29, 2020
This works:

module test;

void foo(T)(T a, T b) {}
void foo(T)(char a, T b) {}

template InstantiateTemplateAt(alias Module, string name, int index, T...) {
    alias Template = __traits(getOverloads, test, name, true)[index];
    alias InstantiateTemplateAt = Template!(T);
}

pragma(msg, typeof(InstantiateTemplateAt!(test, "foo", 1, int)));
// pure nothrow @nogc @safe void(char a, int b)

class Matrix(T) {}

Matrix!T times(T)(Matrix!T a, T b);
// Matrix!T times(T)(T a, Matrix!T b); // <-- second 'times' overload

pragma(msg, typeof(InstantiateTemplateAt!(test, "times", 0, int)));
// Matrix!int(Matrix!int a, int b)

But if I uncomment the second 'times' function template, I get an error:

templateoverloads.d(8): Error: template `test.times` matches more than one template declaration:
templateoverloads.d(16):     `times(T)(Matrix!T a, T b)`
and
templateoverloads.d(17):     `times(T)(T a, Matrix!T b)`
templateoverloads.d(19): Error: template instance `test.InstantiateTemplateAt!(test, "times", 0, int)` error instantiating
_error_

I may be missing the obvious...or it's a compiler bug???
(not sure if this belongs to the Learn section either)


July 30, 2020
On Wednesday, 29 July 2020 at 23:57:21 UTC, Jean-Louis Leroy wrote:
> This works:
>
> [...]
> I may be missing the obvious...or it's a compiler bug???

Yes and it's just been fixed, see https://github.com/dlang/dmd/pull/11431.
So uncommenting the second times works on ~master.


July 30, 2020
On Thursday, 30 July 2020 at 00:27:49 UTC, user1234 wrote:
> On Wednesday, 29 July 2020 at 23:57:21 UTC, Jean-Louis Leroy wrote:
>> This works:
>>
>> [...]
>> I may be missing the obvious...or it's a compiler bug???
>
> Yes and it's just been fixed, see https://github.com/dlang/dmd/pull/11431.
> So uncommenting the second times works on ~master.

Aaaaah thanks! In the meantime I realized that picking the overload at index 1 works. That's the difference between 'foo' and 'times'. Then I was 80% sure it was a bug.