上下文:我处理遗留代码,虽然我想慢慢地使这段代码符合 c++14 标准,但我仍然坚持使用像 printf 这样的函数。 因此这种代码(简化):
#include <iostream>
#include <string>
#include <cstdio>
template <typename... Args>
const char* Format(const char* strFormat, Args... args)
{
static char szBuffer[10000];
auto len = std::vsnprintf(szBuffer, 10000, strFormat, args...);
if (len < 0 || 10000 <= len)
{
szBuffer[0] = 0;
}
return szBuffer;
}
int main()
{
auto test = Format("%s %s %d", "test", "test", 42);
}
奇怪的是,这不起作用:gcc&VS2013 无法从扩展参数包转到 va_arg。 ( http://cpp.sh/4aue )
10:66: error: cannot convert 'const char*' to '__va_list_tag*' for argument '4' to 'int vsnprintf(char*, size_t, const char*, __va_list_tag*)'
为什么会出现这种行为? 同时奇怪地寻找中间 var_arg 函数编译并正常工作(http://cpp.sh/2ggms)。
谢谢
最佳答案
您需要 snprintf
,而不是 vsnprintf
。参数包扩展为以逗号分隔的列表。这些函数的 v
版本只接受一个 va_list
。
在查看您的代码示例时,如果您打算传递 std::string
,您仍然需要像 transform_to_c
这样的函数。
旁注:您应该考虑此代码是否需要线程安全或可重入。使用静态缓冲区意味着两者都不是。
关于c++ - 从参数包到可变参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38896833/