我们总是需要格式化字符串。如果能够说:
std::string formattedStr = format("%s_%06d.dat", "myfile", 18); // myfile_000018.dat
是否有 C++ 方法可以做到这一点?我考虑过的一些替代方案:
snprintf
:使用原始char
缓冲区。在现代 C++ 代码中不太好。std::stringstream
:不支持格式模式字符串,相反,您必须将笨拙的 iomanip 对象推送到流中。boost::format
:使用%
的临时运算符重载来指定参数。丑。
现在我们有了 C++11,难道没有更好的可变参数模板方法吗?
最佳答案
它当然可以用可变参数模板用 C++11 编写。最好包装已经存在的东西,而不是尝试自己编写整个东西。如果您已经在使用 Boost,那么像这样包装 boost::format
非常简单:
#include <boost/format.hpp>
#include <string>
namespace details
{
boost::format& formatImpl(boost::format& f)
{
return f;
}
template <typename Head, typename... Tail>
boost::format& formatImpl(
boost::format& f,
Head const& head,
Tail&&... tail)
{
return formatImpl(f % head, std::forward<Tail>(tail)...);
}
}
template <typename... Args>
std::string format(
std::string formatString,
Args&&... args)
{
boost::format f(std::move(formatString));
return details::formatImpl(f, std::forward<Args>(args)...).str();
}
您可以按照自己的方式使用它:
std::string formattedStr = format("%s_%06d.dat", "myfile", 18); // myfile_000018.dat
如果您不想使用 Boost(但您确实应该使用),那么您也可以包装 snprintf
。它有点复杂且容易出错,因为我们需要管理字符缓冲区和旧式非类型安全可变长度参数列表。使用 unique_ptr
会变得更简洁:
#include <cstdio> // snprintf
#include <string>
#include <stdexcept> // runtime_error
#include <memory> // unique_ptr
namespace details
{
template <typename... Args>
std::unique_ptr<char[]> formatImplS(
size_t bufSizeGuess,
char const* formatCStr,
Args&&... args)
{
std::unique_ptr<char[]> buf(new char[bufSizeGuess]);
size_t expandedStrLen = std::snprintf(buf.get(), bufSizeGuess, formatCStr, args...);
if (expandedStrLen >= 0 && expandedStrLen < bufSizeGuess)
{
return buf;
} else if (expandedStrLen >= 0
&& expandedStrLen < std::numeric_limits<size_t>::max())
{
// buffer was too small, redo with the correct size
return formatImplS(expandedStrLen+1, formatCStr, std::forward<Args>(args)...);
} else {
throw std::runtime_error("snprintf failed with return value: "+std::to_string(expandedStrLen));
}
}
char const* ifStringThenConvertToCharBuf(std::string const& cpp)
{
return cpp.c_str();
}
template <typename T>
T ifStringThenConvertToCharBuf(T const& t)
{
return t;
}
}
template <typename... Args>
std::string formatS(std::string const& formatString, Args&&... args)
{
// unique_ptr<char[]> calls delete[] on destruction
std::unique_ptr<char[]> chars = details::formatImplS(4096, formatString.c_str(),
details::ifStringThenConvertToCharBuf(args)...);
// string constructor copies the data
return std::string(chars.get());
}
snprintf
和 boost::format
在格式规范方面存在一些差异,但您的示例适用于两者。
关于c++ - 如何创建可变参数模板字符串格式化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23742516/