template statistics

Adam D. Ruppe destructionator at gmail.com
Wed Jun 3 21:02:06 UTC 2020


On Wednesday, 3 June 2020 at 19:59:31 UTC, Walter Bright wrote:
> On 6/3/2020 1:06 AM, Nick Treleaven wrote:
>> How does writeln tell that a zero terminated string has been 
>> passed?
>
> D string literals always have a 0 terminator.

Indeed, but the type system cannot distinguish literals from 
other strings.

That is, from writeln's perspective:

writeln("hello");

and

writeln("hello world"[0 .. 5]);

are indistinguishable, yet the latter is not zero terminated.

That's why writeln cannot just blindly forward to puts. It may be 
able to forward to fwrite() though... which is very close to what 
it actually does, it just checks some edge cases along the way 
which has a tiny cost in this case but buys us capabilities in 
other cases.


More information about the Digitalmars-d mailing list