如何在没有 运行 的情况下计算出 `fmt::format` 结果的长度?

How to figure out the length of the result of `fmt::format` without running it?

在回答 关于将二维字符串数组打印到 table 的问题时,我意识到:

我还没有找到更好的方法来确定 fmt::format 调用结果的长度,以实际格式化为字符串并检查该字符串的长度。

这是设计使然,还是有更有效的方法来解决这个问题?我不太了解 fmtlib 的内部结构,但我想,在内存分配发生之前,结果的长度是已知的。我特别想避免内存分配。

直接来自API documentation

template<typename ...T>  
auto fmt::formatted_size(format_string<T...> fmt, T&&... args) -> size_t  

Returns the number of chars in the output of format(fmt, args...).