## digitalmars.D.learn - enum in template

• Sam Hu (15/15) Apr 29 2009 Hello everybody!
• Daniel Keep (10/31) Apr 29 2009 No need to be so formal. Also keep in mind that "sir" only applies to
• Sam Hu (6/6) Apr 29 2009 Thank you very much Daniel!
• bearophile (4/7) Apr 30 2009 For the original poster: this is true in D2+ only, not in D1.
Sam Hu <samhudotsamhu gmail.com> writes:
```Hello everybody!

Convert integral value to string literal:

template myToString(ulong n,
string suffix=n>uint.max?"UL":"U"
{
static if (n<10)
enum myToString=cast(char)(n+'0')-suffix; //q1
else
enum myToString=.myToString!(n/10,"")-
.myToString!(n%10,"")-suffix;            //q2

Here is my questions,sir:
q1.what the key word enum here is doing? not key word char[] or 'string' or
something else?
q2. How does this works?Say n=12,then how can the result be "12"?

Thanks and best regards,
Sam
```
Apr 29 2009
Daniel Keep <daniel.keep.lists gmail.com> writes:
```Sam Hu wrote:
Hello everybody!

Convert integral value to string literal:

template myToString(ulong n,
string suffix=n>uint.max?"UL":"U"
{
static if (n<10)
enum myToString=cast(char)(n+'0')-suffix; //q1
else
enum myToString=.myToString!(n/10,"")-
.myToString!(n%10,"")-suffix;            //q2

Here is my questions,sir:

No need to be so formal.  Also keep in mind that "sir" only applies to
men, and is thus excluding any women in this NG.  :P

q1.what the key word enum here is doing? not key word char[] or 'string' or
something else?

enum defines a "manifest constant."  In other words, it defines a
constant that does NOT consume any storage anywhere in the program: it
exists only at compile-time.

enum blah = 42; // single manifest constant

enum { blah = 42 } // again, but in a block, potentially with others

enum Stuff { blah = 42 } // again, but in a named enumeration

q2. How does this works?Say n=12,then how can the result be "12"?

Recursion.  I assume you have modified the code from its original since
'-' would be invalid.  It should be '~' which is the concatenation
operator in D.

Thanks and best regards,
Sam

-- Daniel
```
Apr 29 2009
Sam Hu <samhudotsamhu gmail.com> writes:
```Thank you very much Daniel!
For q1:clear;
For q2: You are right,it should be "~" other than "-" since it is not very
clear from the vidio of Walter's metaprogramming In D:
http://www.vimeo.com/4333802

Thanks again.
Sam
```
Apr 29 2009
bearophile <bearophileHUGS lycos.com> writes:
```Daniel Keep:
enum defines a "manifest constant."  In other words, it defines a
constant that does NOT consume any storage anywhere in the program: it
exists only at compile-time.

For the original poster: this is true in D2+ only, not in D1.

Bye,
bearophile
```
Apr 30 2009