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);
    }


  • Compile time for... Ada via Digitalmars-d-learn
    • Re: Compile... Ada via Digitalmars-d-learn
    • Re: Compile... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
      • Re: Com... Ada via Digitalmars-d-learn
        • Re:... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
          • ... Ada via Digitalmars-d-learn
            • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
              • ... Ada via Digitalmars-d-learn
                • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

Reply via email to