www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Typeless identifiers in templates

reply BCS <BCS_member pathlink.com> writes:
I am considering a template project that will require a template to specialize
on an identifier that has no other meaning, I would like to do something like
this:

template foo(alias ID1, alias ID2)
{
const int foo = bar!(ID1) + bar!(ID2);
}

template bar(alias ID : A){  const int bar = 1;  }
template bar(alias ID : B){  const int bar = 2;  }

void fn()
{
int i = foo!(A,B); // resolves to 3
}

what type should A, B, etc. be? Really all they are is compile time names. What
would be nice is a "template name" type of some sort. Any ideas for a better
solution?
Apr 22 2006
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <BCS_member pathlink.com> wrote in message 
news:e2ejn2$k2d$1 digitaldaemon.com...
 what type should A, B, etc. be? Really all they are is compile time names. 
 What
 would be nice is a "template name" type of some sort. Any ideas for a 
 better
 solution?
I've done this with strings before, like template foo(char[] ID1, char[] ID2) { const int foo = bar!(ID1) + bar!(ID2); } template bar(char[] ID : "A"){ const int bar = 1; } template bar(char[] ID : "B"){ const int bar = 2; } void fn() { int i = foo!("A", "B"); // resolves to 3 writefln(i); } That works.
Apr 22 2006
parent BCS <BCS_member pathlink.com> writes:
In article <e2ensb$p0v$1 digitaldaemon.com>, Jarrett Billingsley says...
"BCS" <BCS_member pathlink.com> wrote in message 
news:e2ejn2$k2d$1 digitaldaemon.com...
 what type should A, B, etc. be? Really all they are is compile time names. 
 What
 would be nice is a "template name" type of some sort. Any ideas for a 
 better
 solution?
I've done this with strings before, like
I'm using enums, there not that elegant but they work.
template foo(char[] ID1, char[] ID2)
{
 const int foo = bar!(ID1) + bar!(ID2);
}

template bar(char[] ID : "A"){  const int bar = 1;  }
template bar(char[] ID : "B"){  const int bar = 2;  }

void fn()
{
 int i = foo!("A", "B"); // resolves to 3
 writefln(i);
}

That works.
Apr 23 2006