Thread overview | |||||
---|---|---|---|---|---|
|
April 23, 2006 Typeless identifiers in templates | ||||
---|---|---|---|---|
| ||||
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? |
April 23, 2006 Re: Typeless identifiers in templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | "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. |
April 23, 2006 Re: Typeless identifiers in templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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. > > |
Copyright © 1999-2021 by the D Language Foundation