Motivation: I want to leverage the static (compile-time) error checking for string formatting from _format
but cannot call it normally. My ideal solution would be a macro that will validate a static format string literal and the associated arguments, such as:
#include <fmt/format.h>
using namespace fmt::literals;
#define ENSURE_VALID(message, ...) message##_format(__VA_ARGS__)
// Examples
ENSURE_VALID("{} {}", 4, 5); // Will compile fine
ENSURE_VALID("{} {}", 3); // Will fail during compilation because not enough
// arguments were passed for the format string
Problem: The above implementation works for normal situations, but fails when the format string is defined elsewhere:
#define FMT_STR "{} {}"
ENSURE_VALID(FMT_STR, 4, 5); // Fails to compile
Calling ENSURE_VALID
with FMT_STR
fails because the preprocessor evaluates ENSURE_VALID
before FMT_STR
, resulting in FMT_STR_format
, which is not expanded correctly.
What I am looking for: an implementation of ENSURE_VALID
that correctly handles defined strings like FMT_STR
.