Bug 1900 – Template overload sets & traits templates

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-03-09T04:24:28Z
Last change time
2019-08-20T11:58:15Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Bill Baxter
Depends on
10658

Comments

Comment #0 by wbaxter — 2008-03-09T04:24:28Z
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?
Comment #4 by k.hara.pg — 2013-02-14T08:05:16Z
Comment #5 by github-bugzilla — 2013-07-16T18:35:45Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/e59a33f2b639dc9a6eb431ebcf694f533916f038 fix Issue 1900 - Template overload sets & traits templates https://github.com/D-Programming-Language/dmd/commit/cc6b7372f2efffd4c5c0314373af3b595ba06a2c Merge pull request #1660 from 9rnsr/fix1900 Issue 1900 & 8352 & 9235 - Implement Template Overload Set
Comment #6 by k.hara.pg — 2013-07-17T00:10:39Z
Template overload set has been implemented in git head. But, merging overload set by using alias declaration is not yet implemented. See bug 10658.