c++ - 从参数包到可变参数

标签 c++ c++11

上下文:我处理遗留代码,虽然我想慢慢地使这段代码符合 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/

相关文章:

c++ - 如何获得 "hardware time"

C++将CSV之类的数据保存在更大的文件中

c++ - 我应该使用文件系统还是数据库将我的数据存储在我的音乐识别应用程序中?

c++ - 从 C++11 中的 std::exception 派生时的异常规范

c++ - 理解 C++ 字符串

c++ - 使用 std::lock (c++11) 的大量 CPU 负载

c++ - 使用 qprocess 运行 .exe

c++ - c++中的变体成员是什么?

c++ - 为什么我的终止处理程序从未被调用?

c++ - std::lock_guard 不会解锁