www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1900] New: Template overload sets & traits templates

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1900

           Summary: Template overload sets & traits templates
           Product: D
           Version: 2.012
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: wbaxter gmail.com


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".)


-- 
Mar 09 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1900





------- Comment #1 from samukha voliacable.com  2008-08-09 06:20 -------
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


-- 
Aug 09 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1900


Eldar Insafutdinov <e.insafutdinov gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |e.insafutdinov gmail.com


--- Comment #2 from Eldar Insafutdinov <e.insafutdinov gmail.com> 2009-11-09
22:34:01 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 09 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1900



--- Comment #3 from Bill Baxter <wbaxter gmail.com> 2009-11-10 03:59:01 PST ---
(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? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 10 2009