www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Picking function templates with __traits(getOverloads, ..., true)

reply Jean-Louis Leroy <jl leroy.nyc> writes:
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)
Jul 29 2020
parent reply user1234 <user1234 12.de> writes:
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.
Jul 29 2020
parent Jean-Louis Leroy <jl leroy.nyc> writes:
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.
Jul 29 2020