The more interesting question

H. S. Teoh hsteoh at quickfur.ath.cx
Wed May 16 14:30:08 PDT 2012


On Wed, May 16, 2012 at 11:22:48PM +0200, Alex Rønne Petersen wrote:
> On 16-05-2012 23:09, Steven Schveighoffer wrote:
> >On Wed, 16 May 2012 17:06:41 -0400, Alex Rønne Petersen <alex at lycus.org>
> >wrote:
[...]
> >>void myLog(string msg)
> >>{
> >>printf(msg.ptr);
> >>}
> >>
> >>(Which works as expected because string literals are null-terminated.
> >>This is also how things work when you pass a string literal to a
> >>const(char)* value; it just does "literal".ptr.)
> >
> >No, it doesn't:
> >
> >myLog("abc"[0..1]); // prints abc instead of the requested a
> >
> >string is not necessarily a literal. A literal has a special polysemous
> >type, and special properties. An ordinary string does not.
> >
> >-Steve
> 
> I was referring to: myLog("abc");
> 
> When you start bringing slicing into the mix, you're bound to make C
> interop harder and more error-prone because of null-termination.
[...]

I think his point is that myLog is poorly written because it declares
itself to have a string parameter, yet does not function properly when
called with a string that isn't NULL-terminated (e.g. a string slice).


T

-- 
Let's not fight disease by killing the patient. -- Sean 'Shaleh' Perry


More information about the Digitalmars-d mailing list