int to char at compile time

BCS ao at pathlink.com
Tue Mar 20 15:38:45 PDT 2007


Reply to Hendrik,

> hello,
> 
> i am trying something like that:
> 
> char[] foo(int level, char[] msg)
> {
> static c = toString(level);
> return "writefln(\"%d: %s\""~ c ~", \"" ~ msg ~ "\");";
> }
> int main(char[][] args)
> {
> mixin(foo(100, "Test"));
> return 1;
> }
> but gdmd complaints that toString() cannot be evaluated at compile
> time. is there a way to convert the int to char at compile time?
> 
> regards,
> Hendrik


Don Clugston's decimaldigit and itoa, see http://trac.dsource.org/projects/ddl/browser/trunk/meta/conv.d

template decimalDigit(int n)
{
  const char[] decimalDigit = "0123456789"[n..n+1];
} 

template itoa(long n)
{   
  static if (n < 0)
     const char[] itoa = "-" ~ itoa!(-n);  
  else static if (n < 10)
     const char[] itoa = decimalDigit!(n); 
  else
     const char[] itoa = itoa!(n/10L) ~ decimalDigit!(n%10L); 
}




More information about the Digitalmars-d-learn mailing list