digitalmars.D.learn - Typeless identifiers in templates
- BCS (16/16) Apr 22 2006 I am considering a template project that will require a template to spec...
- Jarrett Billingsley (15/20) Apr 22 2006 I've done this with strings before, like
- BCS (2/22) Apr 23 2006
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
"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
In article <e2ensb$p0v$1 digitaldaemon.com>, Jarrett Billingsley says..."BCS" <BCS_member pathlink.com> wrote in message news:e2ejn2$k2d$1 digitaldaemon.com...I'm using enums, there not that elegant but they work.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, liketemplate 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