template template parameter

Ali Çehreli acehreli at yahoo.com
Wed Mar 30 20:39:07 PDT 2011


On 03/30/2011 04:32 PM, Caligo wrote:
> I have a struct that looks something like this:
>
> struct Box(T, size_t width, size_t height){
>
> alias width width_;
> alias height height_;
>
> //do something with a Box of different size
>    void Fun( B )(B b){
>      // using width and height of b
>      B.width_;
>      B.height_;
>    }
> }
>
> auto b1 = Box!(double, 2, 7)();
> auto b2 = Box!(double, 3, 4)();
> b1.Fun(b2);
>
> I think the technical name for this is template template parameter.
> Using the above I'm able to do what I need to do, but is there a
> better way?
> and should I use alias or enum?

Template template parameter means exactly that: a template parameter is 
itself a template. See ContainerType below:

struct SomeContainer(T)
{
     T[] elements;
}

struct SomeOtherContainer(T)
{
     struct List(T)
     {}

     // e.g. this one may use a linked list
     List!T elements;
}

struct Foo(alias ContainerType, T)
{
     // Instantiate the 'template template parameter'
     ContainerType!T member;
}

void main()
{
     Foo!(SomeContainer, double) foo1;
     Foo!(SomeOtherContainer, int) foo2;
}

ContainerType is a template parameter of Foo and is itself a template.

And 'alias' seems to work...

Ali


More information about the Digitalmars-d-learn mailing list