define new types within templates

Phil Deets pjdeets2 at gmail.com
Tue Dec 29 15:24:28 PST 2009


On Tue, 29 Dec 2009 17:44:18 -0500, teo <teo.ubuntu.remove at yahoo.com>  
wrote:

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

Did you try string mixins? Maybe something like this untested code:

template MyTemplate(T)
{
   mixin (CtfeReplace(q{
     struct _T_Struct
     {
       int x;
     }
   }, "_T_", T.stringof));
   mixin (CtfeReplace(q{
     class _T_Class
     {
       void bar(_T_Struct t)
       {
         // ...
       }
     }
   }, "_T_", T.stringof));
}

Sorry if this is a duplicate post. I attempted to cancel my earlier post.


More information about the Digitalmars-d-learn mailing list