[Issue 10724] Allow slice of string literal to convert to const(char)*

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jul 28 03:52:47 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10724



--- Comment #3 from yebblies <yebblies at gmail.com> 2013-07-28 20:52:45 EST ---
(In reply to comment #2)
> Implicit conversions introduce a bit of dangers in a language. They should be
> minimized.
> 
> Instead of this:
> const(char)* s = "abc"[0..$-1];
> 
> What about this?
> const(char)* s = "abc"[0..$-1].ptr;

The conversion is only safe because the string literal is null-terminated. 
Explicitly adding .ptr bypasses that, making it unsafe to rely on this.

Currently `"abc"[0..$-1]` gets const-folded to (essentially)
`cast(string)"ab"`, and not for any reason I can see.  It should be just plain
"ab", which can convert to a const cstring.

The only downside I can see is potential confusion that the above works, but
this doesn't:
string str = "abc"[0..$-1];
const(char)* s = str;

But this is already present with plain string literals, as well as
concatenation and probably others.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list