[Issue 11837] String literals should convert to const(void)*

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Dec 29 09:55:38 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=11837



--- Comment #7 from yebblies <yebblies at gmail.com> 2013-12-30 04:55:36 EST ---
(In reply to comment #6)
> > 
> > memcmp(ptr, x.ptr, 4); // oops, no guarantee x is 0-terminates, but the
> > compiler has no way to know that's what you wanted.
> 
> This may be a bit off topic, but what is the rationale behind this behavior?
> Why can't *all* string literals be 0 terminated, even if you explicitly extract
> a pointer out of them with ".ptr" ?
> 

All string literals are guaranteed to be 0 terminated, even if you use .ptr on
them.  The think is, manifest constants that expand to string literals also
behave like this, so if this compiles you know it is safe:

printf(formatstr, ...);

But in this case, you can't tell:

printf(formatstr.ptr, ...); // was it really a string literal?

> 
> If a string literal implicitly casts to "const(char)*", then it absolutely 100%
> must be implicitly castable to "const(void)*". It only makes sense.
> 

Ok, good.  This is pretty much just convenience for porting c/c++ code, and
removing what I see as an unnecessary limitation.

> Though personally, I find that the fact that you can *implicitly* extract any
> pointer from a string literal to be suboptimal :/

If it's safe, I don't see the harm.

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


More information about the Digitalmars-d-bugs mailing list