Poll: Primary D version

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun May 23 06:33:32 PDT 2010


On 05/23/2010 12:30 AM, Rainer Deyke wrote:
> On 5/22/2010 23:16, Mike Parker wrote:
>> That's not the problem. The problem is this:
>>
>> const(char)* toStringz(const(char)[] s);
>>
>> There's no equivalent for:
>>
>> char *toStringz(char[] s);
>>
>> Hence the need to cast away const or use a wrapper for non-const char*
>> args.
>
> There is no way to define this function with the correct semantics in D.
>   'toStringz' must append a null character to the string, therefore it
> cannot return a pointer to the original string data in the general case.
>   If you pass the resulting string to a function that mutates it, then
> the changes will not be reflected in the original string.
>
> If you pass the resulting string to a function that does /not/ mutate
> it, then that function should be defined to take a 'const char *'.

There is a way, you could simply allocate a copy plus the \0 on the GC 
heap. In fact that's what happens right now.

Andrei


More information about the Digitalmars-d mailing list