www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Constant folding for array casts

reply Don Clugston <dac nospam.com.au> writes:
Most casts work at compile time. This particular one does not.
(it complains that it's a non-constant expression).
-------------
const char[] tapir = "some horned animal";

const byte [] antelope = cast(byte []) tapir;

int main() { return 0; }
---------------

The next bit of code works, and achieves the same thing -- but not quite 
at compile time.

const char[] tapir = "some horned animal";

const byte [] antelope;
static this()
{
  antelope = cast(byte []) tapir;
}
Jan 25 2006
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Don Clugston schrieb am 2006-01-25:
 Most casts work at compile time. This particular one does not.
 (it complains that it's a non-constant expression).
 -------------
 const char[] tapir = "some horned animal";

 const byte [] antelope = cast(byte []) tapir;

 int main() { return 0; }
 ---------------

 The next bit of code works, and achieves the same thing -- but not quite 
 at compile time.

 const char[] tapir = "some horned animal";

 const byte [] antelope;
 static this()
 {
   antelope = cast(byte []) tapir;
 }
Added to DStress as http://dstress.kuehne.cn/run/c/cast_31_A.d http://dstress.kuehne.cn/run/c/cast_31_B.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFD4nln3w+/yD4P9tIRAs3JAKC7L7nyZLyK5Q1gzX5JFFEiIXPvcwCeNv6x waF36mBB2uhpI6Vz+eH71s8= =MCSW -----END PGP SIGNATURE-----
Feb 04 2006