printf() metaprogramming challenge

Andrei Alexandrescu SeeWebsiteForEmail at erdani.com
Fri May 24 20:19:57 UTC 2019


On 5/24/19 2:39 PM, Walter Bright wrote:
> On 5/24/2019 8:35 AM, Jacob Carlborg wrote:
>> This is kind of nice, but I would prefer to have a complete 
>> implementation written in D (of sprintf) that is @nogc @safe nothrow 
>> and pure. To avoid having to add various hacks to apply these attributes.
> 
> C's sprintf is already @nogc nothrow and pure. Doing our own is not that 
> easy, in particular, the floating point formatting is a fair amount of 
> tricky work.

This 100x. Once C++ variadics were out, everybody and their cat had an 
article about "safely replacing the printf family". Invariably the 
implementations ditched printf and as a consequence were bulky and they 
did awfully with floating point numbers. The right approach in 10% of 
the code is to check the arguments during compilation and then forward 
to the C function. The only remaining slightly tricky part (for printing 
to a string) is figuring out the maximum buffer size needed.


More information about the Digitalmars-d mailing list