Compile time format string check

Ada notemail at not.com
Sun Mar 22 21:57:19 UTC 2026


I have managed to write a function dprintf that will expand the 
string argument to (length, pointer).
But now I wish there was a way to verify the format string if it 
actually matches the rest of the parameters.
%.\*s should match with string
%s should match with char*

Is there a way to do it at compile time without using template 
instantiation?

Here is the code for dprintf (any suggestions for nicer code are 
welcome):

import core.stdc.stdio : printf, fgets, stdin;
import core.stdc.stdlib : malloc;
import core.stdc.string : memcpy;
import std.typecons : tuple;

     auto expandArg(T)(T arg)
     {
         static if (is(T == string))
             return tuple(cast(int)arg.length, arg.ptr);
         else
             return tuple(arg);
     }

     auto expandAll()
     {
         return tuple();
     }

     auto expandAll(T, Rest...)(T first, Rest rest)
     {
         return tuple(
             expandArg(first).expand,
             expandAll(rest).expand
         );
     }

     void dprintf(Args...)(const char* fmt, Args args)
     {
         auto expanded = expandAll(args);
         printf(fmt, expanded.expand);
     }




More information about the Digitalmars-d-learn mailing list