define new types within templates

BCS none at anon.com
Tue Dec 29 18:52:22 PST 2009


Hello teo,

> There was a way to define new types within templates and I think that
> I have seen that demonstrated here in the newsgroups, but cannot find
> it now. Can someone help me please?
> 
> I would like to do something like this:
> 
> template MyTemplate(T)
> {
> struct T ~ "Struct"  // define FooStruct structure
> {
> int x;
> }
> class T ~ "Class"  // define FooClass class
> {
> void bar(T ~ "Struct" t)
> {
> // ...
> }
> }
> }
> void main()
> {
> mixin MyTemplate!("Foo");
> FooStruct t;
> FooClass f = new FooClass();
> f.bar(t);
> }
> Hopefully I am not mistaken.
> 


what I would do is this:

template MyTemplate(char[] name)
{
    struct S { int x; }
    class C { ... }

    // put the minimum you can in the string mixin.
    mixin ("alias S "~name~"Struc; alias C "~name~"Class;");
}




More information about the Digitalmars-d-learn mailing list