Compile-time int-to-string conversion

Robert Fraser fraserofthenight at gmail.com
Wed May 6 02:05:02 PDT 2009


Lars T. Kyllingstad wrote:
> How can one, in D2, at compile time, convert an integer value (or any 
> other type, for that matter) to a string? Here's a simplified example of 
> what I want to do:
> 
>     import std.conv;
> 
>     template Say(int N)
>     {
>         pragma(msg, to!string(N));
>     }
> 
>     mixin Say!(123);
> 
> This doesn't compile, because the to!string() function for some reason 
> can't be evaluated at compile time. (I've included the error messages 
> below.)
> 
> In D1 I used to write N.stringof, which worked as expected, but in D2 
> this would just print "N" instead of "123".
> 
> -Lars
> 
> 
> Compile errors:
> /usr/local/include/d/druntime/core/memory.di(162): Error: cannot 
> evaluate gc_malloc(sz,ba) at compile time
> /usr/local/include/d/phobos2/std/conv.d(2402): Error: cannot evaluate 
> malloc(12u,2u) at compile time
> /usr/local/include/d/phobos2/std/conv.d(2454): Error: cannot evaluate 
> to(cast(uint)value) at compile time
> tmp.d(5): Error: cannot evaluate to(123) at compile time
> tmp.d(5): Error: pragma msg string expected for message, not 'to(123)'
> 
> tmp.d(8): Error: mixin tmp.Say!(123) error instantiating

public template Digit(uint n)
{
	public static const char[] Digit = "0123456789"[n .. n+1];
}

public template Itoa(uint n)
{
	static if(n < 0)
		public static const char[] Itoa = "-" ~ Itoa!(-n);
	else static if (n < 10)
		public static const char[] Itoa = Digit!(n);
	else
		public static const char[] Itoa = Itoa!(n / 10) ~ Digit!(n % 10);
}

template Say(int N)
{
	pragma(msg, Itoa!(N));
}

In D2, replace "const" with "enum" in those declarations if you don't 
want storage allocated for the intermediate results.


More information about the Digitalmars-d-learn mailing list