Can we check the arguments to format() at compile time?

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 2 08:44:41 PDT 2016


On 04/01/2016 07:26 PM, Alex Parrill wrote:
> On Friday, 1 April 2016 at 21:25:46 UTC, Yuxuan Shui wrote:
>> Clang has this nice feature that it will warn you when you passed
>> wrong arguments to printf:
>>
>> #include <stdio.h>
>> int main(){
>>     long long u = 10;
>>     printf("%c", u);
>> }
>>
>> clang something.c:
>> something.c:4:15: warning: format specifies type 'int' but the
>> argument has type 'long long' [-Wformat]
>>
>> With the CTFE power of D, we should be able to do the same thing when
>> the format string is available at compile time. Instead of throwing
>> exceptions at run time.
>
> Not as-is, because the format string is a runtime argument and not a
> compile-time constant.
>
> Consider:
>
>      writefln(rand() >= 0.5 ? "%s" : "%d", 123);
>
> It's certainly possible with a new, templated writef function.
> Hypothetically:
>
>      writefln_ctfe!"%s"(1234); // would fail

What happened to the work that added a compile-time string to writef? -- 
Andrei


More information about the Digitalmars-d mailing list