如何将非可变值传递给 fmt::format?
How to pass not variadic values to fmt::format?
我正在使用伟大的 fmt C++ library 来更优雅地格式化字符串。
我想将一个非变量参数列表传递给 fmt::format
。它可以是 std::vector
,或 std::string
,或其他任何东西,但它总是匹配格式字符串。
所以 fmt::format
的工作方式如下:
std::string message = fmt::format("The answer is {} so don't {}", "42", "PANIC!");
但我想要的是:
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);
有什么方法/解决方法吗?
如果不对 fmt 库进行更改,这似乎是不可能的。 fmt::format
调用 fmt::vformat
,它采用表示多个参数的 fmt::format_args
或 fmt::wformat_args
对象,但是创建 format_args
或 wformat_args
对象的唯一方法是通过另一个可变参数函数,这意味着必须在编译时知道参数的数量和类型。
所以你可以编写一个包装器来解压 std::tuple
或 std::array
并将其元素传递给 fmt::format
,因为这些元素的数量和类型在编译时是已知的.但是你不能对 std::vector
、std::list
等做同样的事情,因为这些容器的大小在运行时可能会发生变化。
添加一个额外的图层,例如:
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>());
}
用法为:
std::vector<std::string> arr = {"42", "PANIC!"};
my_format<2>("The answer is {} so don't {}", arr);
使用 operator ""_format
您可能会在编译时获得有关预期大小的信息
您可以从 vector
中自行构建 vformat
的参数。这似乎有效:
std::string format_vector(std::string_view format,
std::vector<std::string> const& args)
{
using ctx = fmt::format_context;
std::vector<fmt::basic_format_arg<ctx>> fmt_args;
for (auto const& a : args) {
fmt_args.push_back(
fmt::internal::make_arg<ctx>(a));
}
return fmt::vformat(format,
fmt::basic_format_args<ctx>(
fmt_args.data(), fmt_args.size()));
}
std::vector<std::string> args = {"42", "PANIC!"};
std::string message = format_vector("The answer is {} so don't {}", args);
在当前版本 (8.1.1) 中,可以执行以下操作;
fmt::dynamic_format_arg_store<fmt::format_context> ds;
ds.push_back(42);
ds.push_back("PANIC");
std::string msg = fmt::vformat("The answer is {} so don't {}", ds);
我正在使用伟大的 fmt C++ library 来更优雅地格式化字符串。
我想将一个非变量参数列表传递给 fmt::format
。它可以是 std::vector
,或 std::string
,或其他任何东西,但它总是匹配格式字符串。
所以 fmt::format
的工作方式如下:
std::string message = fmt::format("The answer is {} so don't {}", "42", "PANIC!");
但我想要的是:
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);
有什么方法/解决方法吗?
如果不对 fmt 库进行更改,这似乎是不可能的。 fmt::format
调用 fmt::vformat
,它采用表示多个参数的 fmt::format_args
或 fmt::wformat_args
对象,但是创建 format_args
或 wformat_args
对象的唯一方法是通过另一个可变参数函数,这意味着必须在编译时知道参数的数量和类型。
所以你可以编写一个包装器来解压 std::tuple
或 std::array
并将其元素传递给 fmt::format
,因为这些元素的数量和类型在编译时是已知的.但是你不能对 std::vector
、std::list
等做同样的事情,因为这些容器的大小在运行时可能会发生变化。
添加一个额外的图层,例如:
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>());
}
用法为:
std::vector<std::string> arr = {"42", "PANIC!"};
my_format<2>("The answer is {} so don't {}", arr);
使用 operator ""_format
您可能会在编译时获得有关预期大小的信息
您可以从 vector
中自行构建 vformat
的参数。这似乎有效:
std::string format_vector(std::string_view format,
std::vector<std::string> const& args)
{
using ctx = fmt::format_context;
std::vector<fmt::basic_format_arg<ctx>> fmt_args;
for (auto const& a : args) {
fmt_args.push_back(
fmt::internal::make_arg<ctx>(a));
}
return fmt::vformat(format,
fmt::basic_format_args<ctx>(
fmt_args.data(), fmt_args.size()));
}
std::vector<std::string> args = {"42", "PANIC!"};
std::string message = format_vector("The answer is {} so don't {}", args);
在当前版本 (8.1.1) 中,可以执行以下操作;
fmt::dynamic_format_arg_store<fmt::format_context> ds;
ds.push_back(42);
ds.push_back("PANIC");
std::string msg = fmt::vformat("The answer is {} so don't {}", ds);