I'm not sure this kind of thing is supposed to work with template overload sets, but I hope it is supposed to, as it is a pattern I see often in C++ code.
The idea is to define some "Traits" templates that define various compile-time aspects of a particular type.
----
module main;
import std.stdio;
import A;
import B;
import generic;
void main()
{
AClass a;
BClass b;
writefln("Traits!(AClass).name: ", Traits!(AClass).name);
writefln("Traits!(BClass).name: ", Traits!(BClass).name);
writefln("Traits!(int).name: ", Traits!(int).name);
}
----
module A;
class AClass
{
}
template Traits(T : AClass)
{
enum string name = "AClass";
}
----
module B;
class BClass
{
}
string func(BClass b) {
return "BClass";
}
template Traits(T : BClass)
{
enum string name = "BClass";
}
----
module generic;
struct Traits(T)
{
const string name = "any";
}
----
Right now I get:
main.d(18): template instance Traits is not a template declaration, it is a overloadset
main.d(18): Error: undefined identifier Traits!(AClass).name
(Also note the grammar error in the message "it is a overload set".)
Comment #1 by samukha — 2008-08-09T06:20:59Z
At least, there should be a way to merge template overload sets as it is the case with function overload sets.
alias A.Traits Traits;
alias B.Traits Traits;
alias generic.Traits Traits;
Error: declaration Traits is already defined
Another use case:
----
template Foo(T)
{
template Bar(U : T)
{
}
}
mixin Foo!(int) A;
mixin Foo!(char) B;
alias Bar!(int) bar; //error
----
Error: template instance Bar is not a template declaration, it is a overloadset
Comment #2 by e.insafutdinov — 2009-11-09T22:34:01Z
Seems like there has not been an update on this one for quite along time. That is obviously a big issue, there should be a way to merge template overload sets, otherwise some great opportunities in using templates are lost. I am using templates in Qt bindings for specifying some compile-time information for types. My usecase is very similar to that provided by Bill. I would really like for this one to be resolved.
Thanks.
Comment #3 by wbaxter — 2009-11-10T03:59:01Z
(In reply to comment #2)
> Seems like there has not been an update on this one for quite along time. That
> is obviously a big issue, there should be a way to merge template overload
> sets, otherwise some great opportunities in using templates are lost. I am
> using templates in Qt bindings for specifying some compile-time information for
> types. My usecase is very similar to that provided by Bill. I would really like
> for this one to be resolved.
> Thanks.
I think I ended up working around this by putting the essential information into the classes themselves, and then having one Traits template that covered built-in types. Another big template did bunches of static structural tests to tease the needed information out of a class.
In my case that was sufficient. But it's certainly more cumbersome to deal with. Perhaps you can explain your case in a little more detail and we might be able to come up with some workaround?