www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Documentation: example no longer compiles

reply Denton Cockburn <diboss hotmail.com> writes:
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()
Feb 28 2008
parent =?ISO-8859-1?Q?S=F6nke_Ludwig?= writes:
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.
Mar 01 2008