Search code examples
cvariadic-functionsvariadic-macros

How can I guarantee type safety of variadic arguments?


In C, I'd like to make a function or a macro that looks like this:

void Log(char* what, ...) 

where the ... must be key-value pairs of const char*'s. I would really like code that doesn't follow this to blow up at compile time. I tried looking for an __attribute((..)) directive that did this, to no avail. Any ideas?


Solution

  • where the ... must be key-value pairs of const char*'s

    Then you don't want variadic arguments at all. If you know the type(s) you are expecting beforehand then you don't need it and you're just making things more difficult than they need to be.

    Just pass in an array.

    void Log(const char *what, const char **args, size_t size);