How to get a range from std.container.array for use with std.format.sformat?

Steven Schveighoffer schveiguy at yahoo.com
Tue Jan 30 18:42:45 UTC 2018


On 1/30/18 12:53 PM, cc wrote:
>      import std.container;
>      import std.format;
>      Array!char str;
>      str.length = 256;
> 
>      str.sformat!"%s:%s"("some", "string");
>      // Error: template std.format.sformat cannot deduce function from 
> argument types !("%s:%s")(Array!char, string, string), candidates are:
>      //   std.format.sformat(alias fmt, Args...)(char[] buf, Args args) 
> if (isSomeString!(typeof(fmt)))
>      //   std.format.sformat(Char, Args...)(char[] buf, in Char[] fmt, 
> Args args)

sformat requires a builtin array, apparently. It doesn't work for other 
types.

> 
>      auto rng = cast(char[]) str[];
>      // Error: cannot cast expression `str.opSlice()` of type 
> `RangeT!(Array!char)` to `char[]`

why not just auto rng = str[]?

> Additionally, would sformat cause GC allocations even when writing to a 
> preallocated buffer?

Not directly. If sformat needs to call a function on what you are asking 
to format that would use the GC, then it would not be @nogc.

-Steve


More information about the Digitalmars-d-learn mailing list