Documentation: example no longer compiles

Sönke Ludwig ludwig at informatik_dot_uni-luebeck.de
Sat Mar 1 00:42:13 PST 2008


Denton Cockburn wrote:
> This example found at
> http://www.digitalmars.com/d/2.0/templates-revisited.html no longer
> compiles in 2.011:
> 
> template decimalDigit(int n)	// [3]
> {
>   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); 
> }
> 
> char[] foo()
> {
>   return itoa!(264);   // returns "264"
> }
> 
> the fix is to change the signature of 'char[] foo()' to const(char[]) foo()

Or even better, change all 'char[]' to 'string' with respect to D1/2
compatibility.


More information about the Digitalmars-d-bugs mailing list