[Bug 51] New: String cast overrides the char type of decorated string literals.

Thomas Kuehne thomas-dloop at kuehne.cn
Sat Mar 18 23:44:18 PST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

d-bugmail at puremagic.com schrieb am 2006-03-14:
> Observe the following code:
>
>   char[6] cstr = "123456"c;
>   auto str1 = cast(wchar[3])(cstr);
>   auto str2 = cast(wchar[3])("123456"c);
>
>   writefln("str1: ", (cast(char[])str1).length , " : ", (cast(char[])str1));
>   // prints: str1: 6 : 123456
>   writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2)); 
>   // prints: str2: 6 : ?1?2?3
>
> One would expect the same behaviour from str1 and str2, however the
> cast(wchar[3]) in str2 overrides the string decorator 'c', and makes the string
> literal "123456" (i.e. the string "instance" data) be of type wchar[6] instead
> of char[6].

This might seem confusing, but is the correct behaviour.
http://www.digitalmars.com/d/arrays.html

# The type of a string is determined by the semantic phase of
# compilation. The type is one of: char[], wchar[], dchar[], and is
# determined by implicit conversion rules. If there are two equally
# applicable implicit conversions, the result is an error. To
# disambiguate these cases, a cast is appropriate:
#
# cast(wchar [])"abc"	// this is an array of wchar characters

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFEHRi13w+/yD4P9tIRAmgpAJ9F/KNd1JSBTBOp1QME7RA6Lwja9wCfT7Gx
SlkT9jiEQ1rxtIl/cc7wT7s=
=aqz7
-----END PGP SIGNATURE-----



More information about the Digitalmars-d-bugs mailing list