Picking function templates with __traits(getOverloads, ..., true)
Jean-Louis Leroy
jl at leroy.nyc
Wed Jul 29 23:57:21 UTC 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)
More information about the Digitalmars-d-learn
mailing list