Notes from C++ static analysis

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jun 26 19:40:25 PDT 2013


On 6/26/13 12:53 PM, dennis luehring wrote:
> Am 26.06.2013 21:33, schrieb Andrei Alexandrescu:
>> On 6/26/13 11:08 AM, bearophile wrote:
>>> On the other hand this D program prints just
>>> "10" with no errors, ignoring the second x:
>>>
>>> import std.stdio;
>>> void main() {
>>> size_t x = 10;
>>> writefln("%d", x, x);
>>> }
>>>
>>> In a modern statically typed language I'd like such code to give a
>>> compile-time error.
>>
>> Actually this is good because it allows to customize the format string
>> to print only a subset of available information (I've actually used
>> this).
>
> why is there always a tiny need for such tricky stuff - isn't that only
> usefull in very rare cases

This is no tricky stuff, simply allows the user to better separate 
format from data. The call offers the data, the format string chooses 
what and how to show it. Obvious examples include logging lines with 
various levels of detail and internationalized/localized/customized 
messages that don't need to display all data under all circumstances.

Checking printf for undefined behavior and mistakes affecting memory 
safety of the entire program is a noble endeavor, and kudos to the 
current generation of C and C++ compilers that warn upon misuse.

Forcing D's writef to match exactly the format string against the number 
of arguments passed is a waste of flexibility and caters to programmers 
who can't bring themselves to unittest or even look at the program 
output - not even once. Our motivation is to help those out of such 
habits, not support them.


Andrei


More information about the Digitalmars-d mailing list