I'm playing with the great fmt C++ library to format strings more gracefully.
And I'd like to pass a non-variable arguments list to fmt::format
. It could be a std::vector
, or std::string
, or whatever, but it will always match the format string.
So fmt::format
works like:
std::string message = fmt::format("The answer is {} so don't {}", "42", "PANIC!");
But what I'd like is something like:
std::vector<std::string> arr;
arr.push_back("42");
arr.push_back("PANIC!");
std::string message = fmt::format("The answer is {} so don't {}", arr);
Is there a way / workaround to do so?
Add an extra layer, something like:
template <std::size_t ... Is>
std::string my_format(const std::string& format,
const std::vector<std::string>& v,
std::index_sequence<Is...>)
{
return fmt::format(format, v[Is]...);
}
template <std::size_t N>
std::string my_format(const std::string& format,
const std::vector<std::string>& v)
{
return my_format(format, v, std::make_index_sequence<N>());
}
Usage would be:
std::vector<std::string> arr = {"42", "PANIC!"};
my_format<2>("The answer is {} so don't {}", arr);
With operator ""_format
you might have the information about expected size at compile time