Problem with a convoluted templated struct

Kiith-Sa 42 at theanswer.com
Tue Jun 28 19:06:02 PDT 2011


Hello.

I am trying to implement a templated struct containing a
data member created by std.variant.Algebraic instantiated
with the struct's template parameters. 

The problem is, I want to template the struct with e.g. 
arrays (lists, etc.) of its own type. I don't know
any way to do this without ugly string mixin code.

I'm not sure if I can explain this clearly, so here's some code:



import std.variant;

struct Node(Types ...)
{
    Algebraic!(Types) value;
    
    //other members, etc...
}

unittest
{
    //this works
    Node!(string, int) node1;

    //this is what I want, but I can't do it, since
    //Node must be templated
    Node!(Node[], string, int) node1;
}


//Now, I can get around this with a string mixin:
//(just one argument in this example, but it
// could be variadic with more complex code)


struct Node2(string t)
{
    mixin("private alias " ~ t ~ " Type;");
    Algebraic!(Type) value;
}

unittest
{
    //works, but ugly
    Node2!("Node2[]") node;
}



Is there any way to do this without string mixins?
(in case this is proposed: I specifically need structs, not classes.
I know I could do this with templated derived classes storing
an array/list/whatever with parent class type.


Thanks for any help.


More information about the Digitalmars-d-learn mailing list