Why implicit conversion of string literal to char[] does not works?
Michal Minich
michal.minich at gmail.com
Tue Jul 2 06:33:09 PDT 2013
On Tuesday, 2 July 2013 at 13:07:40 UTC, Regan Heath wrote:
> It is done for performance reasons. On UNIX the compiler will
> put the literal "abc" into read only memory. It could/should
> do the same on windows but doesn't yet (I believe).
>
> So, the compiler is treating them as such, by giving them the
> type immutable(char)[] (AKA string).
>
> And, the spec should, if it doesn't, define string literals to
> be immutable.
>
Ok I understand. What I did as a first thing when I get error on
"char[] x = "a" was "char x = cast(char[])"a", Which was
obviously incorrect - as the "a" was/should be placed in rom. So
if this expression is allays wrong - casting string literal to
mutable, then compiler should emit an error on this (If one
implementation (dmd/win) makes this valid, it still doesn't mean
compiler should honor implementation over specification - amusing
it is specified that strings go into rom...)"
Also, given you explanation, I see it as good to type "auto x =
"a" as string, but give explicit notation char[] x = "a" - I
don't see reason to not allow string literal to be typed as both
char[] and string at the same time, and convert to char[] - and
also place in ram, if explicitly asked.
The only reason that comes now to mi mind is when working with
old code base - before auto and immutable(char)[] .. the code had
to use char[] x = "a", but these must be all eliminated by now....
More information about the Digitalmars-d-announce
mailing list