www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - template tempalte arguments

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
At first i missed tempalte template arguments but then alias
tempalte arguments should be their superset. But what am i
doing wrong here:

import std.stdio;

template TypeList(T,U)
{
 struct TypeList
 {
  alias T Head;
  alias U Tail;
 }
}

class NullType{}
alias TypeList!(byte , TypeList!(short, NullType)) IntegerTypes;

template Length(T : NullType)
{
 //enum {value = 0 };
 const int value = 0;
}
template Length(alias TypeList)
{
 //enum {value = 1+Length!(TypeList.Tail).value };
 const int value = 1 + Length!(TypeList.Tail).value;
}

int main ( char [] [] args )
{
 writefln("Typelist length is %d",Length!(IntegerTypes).value);
 //writefln,getch;
 return 1;
}

The problem is in the Length template, if i use enums i get:
E:\D language\testtemplates\templates1\templates1.d(22): undefined
identifier template instance Length!(TypeList ).value
E:\D language\testtemplates\templates1\templates1.d(22): void has no value
E:\D language\testtemplates\templates1\templates1.d(22): incompatible types
for ((1) + (template instance Length!(TypeList ).value)): 'int' and 'void'
E:\D language\testtemplates\templates1\templates1.d(22): Integer constant
expression expected instead of 1 + template instance
Length!(TypeList ).value
E:\D language\testtemplates\templates1\templates1.d(28): template instance
Length!(TypeList ) error instantiating

and if i use const int i get:
E:\D language\testtemplates\templates1\templates1.d(23): non-constant
expression 1 + value

Can this be done in D?
Oct 15 2004
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Ivan Senji wrote:
 At first i missed tempalte template arguments but then alias
 tempalte arguments should be their superset. But what am i
 doing wrong here
It's because you're using "alias." Change Length(alias TypeList) to Length(TypeList), though it would probably be less confusing to make it Length(T). Sean
Oct 15 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Sean Kelly" <sean f4.ca> wrote in message
news:ckpqbl$2vkr$1 digitaldaemon.com...
 Ivan Senji wrote:
 At first i missed tempalte template arguments but then alias
 tempalte arguments should be their superset. But what am i
 doing wrong here
It's because you're using "alias." Change Length(alias TypeList) to Length(TypeList), though it would probably be less confusing to make it Length(T).
Thanks a lot! I was sure i tried that but i didn't. Thanks :)
 Sean
Oct 16 2004
prev sibling parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
Another question(hope this one is as easy to answer as the previous
one)

TypeList template is the same as in my firrst post:

//i think i got this one right
template IndexOf(X: NullType,T)
{
 enum{IndexOf=-1};
}

//but this specialization i don't know how to convert from C++:
template IndexOf(TypeList, T : TypeList.Head ) //templates1.d(41): no
property 'Head' for type 'TypeList'
{
 enum{IndexOf=0};
}

/*
in C++
template<class T, class Tail>
struct IndexOf<TypeList<T,Tail>,T>
{
 enum{value = 0};
};
*/

//this one is ok i think:
template IndexOf(TypeList,T)
{
 enum{temp = .IndexOf!(TypeList.Tail,T)};
 enum{IndexOf = temp ==-1?-1:1+temp};
}

Hope someone can answer :)
Oct 16 2004