Output-Range and char
Mafi via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Apr 23 05:46:57 PDT 2017
On Sunday, 23 April 2017 at 12:03:58 UTC, Ali Çehreli wrote:
> On 04/23/2017 04:17 AM, Mafi wrote:
>
>> /opt/compilers/dmd2/include/std/range/primitives.d(351):
>> Error: static
>> assert "Cannot put a char into a char[]."
>
> Appender recommended:
>
> import std.format, std.stdio, std.array;
>
> void main() {
> auto sink = appender!(char[])();
> formattedWrite(sink, "Long string %s\n", "more more more");
> write(sink.data);
> }
>
> Of course appender!string is more natural but I just wanted to
> see that it works with char[] as well.
>
> Ali
Thank you. I see. But I would really like not to allocate ever.
Instead I want to fill a given buffer, then stop formatting.
I probably have to implement my own output-range for this use
case. It would fill the buffer and then ignore further
'put'-calls. I can live with this.
I do have a follow-up question though. Why does formattedWrite
take the output-range by value? It just doesn't make sense for
value-type output-ranges; especially with consecutive calls. One
has to pass the pointer of the output-range (this does work
because of auto-dereferencing). Forgetting it one place, lets the
code still compile, with very strange semantics which most
probably were not intended.
More information about the Digitalmars-d-learn
mailing list