有没有任何方法可以将varargs与boost::格式一起使用
Is there any way to use varargs with boost::format?
我有点讨厌使用固定大小的缓冲区和vnsprintf常见的怀疑。
像这样的东西是否可以使用boost::format来处理变量参数列表?
遗憾的是,我不能使用C++11中的任何东西。
void formatIt(const char* msg, ...) {
va_list args;
va_start(args, msg);
boost::format f(msg);
for loop somehow {
f % va_arg(args, const char *); //does this work?
}
va_end(args);
}
我使用这个:
inline static std::string FormatStringRecurse(boost::format& message)
{
return message.str();
}
template <typename TValue, typename... TArgs>
std::string FormatStringRecurse(boost::format& message, TValue&& arg, TArgs&&... args)
{
message % std::forward<TValue>(arg);
return FormatStringRecurse(message, std::forward<TArgs>(args)...);
}
template <typename... TArgs>
std::string FormatString(const char* fmt, TArgs&&... args)
{
using namespace boost::io;
boost::format message(fmt);
return FormatStringRecurse(message, std::forward<TArgs>(args)...);
}
我曾经使用过这个:
inline static std::string FormatString()
{
return std::string();
}
inline static std::string FormatString(const char* szMessage)
{
return szMessage;
}
template<typename Type1>
static std::string FormatString(const char* formatString, const Type1& arg1)
{
using namespace boost::io;
boost::format formatter(formatString);
formatter % arg1;
return boost::str( formatter );
}
template<typename Type1, typename Type2>
static std::string FormatString(const char* formatString, const Type1& arg1, const Type2& arg2)
{
using namespace boost::io;
boost::format formatter(formatString);
formatter % arg1 % arg2;
return boost::str( formatter );
}
template<typename Type1, typename Type2, typename Type3>
static std::string FormatString(const char* formatString, const Type1& arg1, const Type2& arg2, const Type3& arg3)
{
using namespace boost::io;
boost::format formatter(formatString);
formatter % arg1 % arg2 % arg3;
return boost::str( formatter );
}
/// etc up to 10 args
我想它没有按照你的要求使用varargs,但你经常需要超过10个左右的参数吗?可能有一些宏观魔法会让你想要的事情发生,但呃。。。这很神奇,所以你最好把所有的版本都写出来。
相关文章:
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- boost::p rogram_options 配置文件格式
- 有没有办法使用 Boost 获取区域时间格式?
- Boost::log:基于级别的不同格式(HTML 格式)
- boost::log 输出到 Visual Studio 输出控制台 - 为格式添加额外的 LF/CR
- 尝试使用 boost::asio 连接到 SOCKS 5 服务器,但我的连接请求格式不正确
- 操作方法:将 boost::endian 缓冲区类型转换回本机格式
- 编译用于创建boost.log格式的代码时出错
- 将Boost序列化文本存档转换为结构化格式
- boost::从 DLL 调用时日志格式不起作用
- Boost ::格式仅在Windows下
- 如何在C++中停止 boost::regex_replace 的格式 ($)
- 使用 boost::regex 更改文件中数据的格式
- 使用boost::propertytree为我的代码输入文件格式
- 如何将标准::字符串格式的 JSON 解码为 Boost 属性树
- 无法在Boost.log中设置控制台日志的格式
- 有没有任何方法可以将varargs与boost::格式一起使用
- 一个方便的C++日志记录语句,使用boost::格式
- c++boost格式float-如何指定我不想要的.以及后面的零
- 为什么 Boost 格式和 printf 在同一格式字符串上的行为不同