简体   繁体   English

使用Variadic模板解压缩参数列表

[英]Unpacking Argument List with Variadic Template

I'm trying to create a C++ convenience wrapper around an old C-API that uses an opaque data type. 我正在尝试围绕使用不透明数据类型的旧C-API创建C ++便捷包装器。 There's one particular C-function which takes a format string, along with a variable number of arguments using the C <stdarg.h> facilities. 有一个特定的C函数,它使用一个格式字符串,以及使用C <stdarg.h>工具的可变数量的参数。 As part of my wrapper, I want to be able to pass an arbitrary number of arguments (including C++ objects) to this function. 作为我的包装器的一部分,我希望能够将任意数量的参数(包括C ++对象)传递给此函数。 However, since obviously the <stdarg.h> facilities can't work with non-POD data, I created a templated conversion function which converts C++ objects (like std::string ) into POD equivalents. 但是,因为很明显<stdarg.h>工具无法处理非POD数据,所以我创建了一个模板化转换函数,它将C ++对象(如std::string )转换为POD等价物。

I thought this whole thing would be an easy exercise using C++0x variadic templates, but I'm having difficulty figuring out how to write this function in a way that properly unrolls the argument pack while applying my conversion function to each argument. 我认为整个事情使用C ++ 0x可变参数模板是一个简单的练习,但是我很难弄清楚如何在将转换函数应用于每个参数时正确地展开参数包的方式编写此函数。

What I have so far is: 到目前为止我所拥有的是:

   template <class T, class... Args>
   void apply(OPAQUE* object, const char* fmt_string, T&& val, Args&&... args)
   {
      apply(object, fmt_string, Convert(val), args...);
   }

   template <class... Args>
   void apply(OPAQUE* object, const char* fmt_string, Args&&... args)
   {
      C_API_Function_Call(object, fmt_string, args...);
   }

Of course, this doesn't work because the recursive function call never actually unpacks the Args... , so it just recurses until the stack overflows. 当然,这不起作用,因为递归函数调用永远不会实际解包Args... ,所以它只是递归直到堆栈溢出。 I can't figure out how to make it unpack the arguments while also passing the current argument to the Convert function, and then recursively passing along the result. 我无法弄清楚如何将它解包参数, 同时将当前参数传递给Convert函数,然后递归传递结果。

Is there anyway to do this? 反正有没有这样做?

I think you need the same syntax as when you do perfect forwarding : 我认为您需要与完成转发时相同的语法:

template <class... Args>
void apply(OPAQUE* object, const char* fmt_string, Args&&... args)
{
   C_API_Function_Call(object, fmt_string, Convert(std::forward<Arg>(args))...);
}

The ellipsis ... can be placed at the right of an expression containing the argument pack, and not only directly at the right of the argument pack itself. 省略号...可以放在包含参数包的表达式的右侧,而不仅是直接位于参数包本身的右侧。

So, : 所以,:
func(args...) expand to func(arg1, arg2, arg3, [...] , argN) func(args ...)展开到func(arg1,arg2,arg3,[...],argN)
func(args)... expand to func(arg1), func(arg2), [...] , func(argN) func(args)...扩展为func(arg1),func(arg2),[...],func(argN)

At the moment C++0x draft does not provide any solution for that. 目前C ++ 0x草案没有为此提供任何解决方案。 Because problem is similar to expanding tuples, you can read this discussion and write similar expanding function. 因为问题类似于扩展元组,所以您可以阅读此讨论并编写类似的扩展函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM