【发布时间】:2019-10-29 00:40:42
【问题描述】:
使用 C++ fmt 库,并给定一个裸格式说明符,有没有办法使用它来格式化单个参数?
// example
std::string str = magic_format(".2f", 1.23);
// current method
template <typename T>
std::string magic_format(const std::string spec, const T arg) {
return fmt::format(fmt::format("{{:{}}}", spec), arg);
}
虽然上述实现符合我的要求,但我更喜欢一种不需要我在此过程中构建新的临时字符串的方式。
【问题讨论】:
标签: c++ formatting fmt