I want to take a variable number of function arguments, and in the function itself, wrap each function argument using a template wrapper class and pass these wrapper classes as arguments to another function.
我想获取可变数量的函数参数,并在函数本身中,使用模板包装类包装每个函数参数,并将这些包装类作为参数传递给另一个函数。
Say if I have a template class that simply wraps a variable.
假设我有一个简单包装变量的模板类。
template<class T>
class Wrapper
{
public:
Wrapper(T t) : t_(t)
.
.
{}
private:
T t_;
}
And I have a function f that calls function g, passing in Wrapper classes for each argument of f to g.
我有一个调用函数g的函数f,为f到g的每个参数传递Wrapper类。
template <typename T1, typename T2, typename T3>
void f(T1 a, T2 b, T3 c)
{
g(Wrapper<T1>(a), Wrapper<T2>(b), Wrapper<T3>(c));
}
Function g is not important here, but could be for example, a sequence of overloaded functions, each with a different number and type of Wrapper classes as its parameters.
函数g在这里并不重要,但可以是例如一系列重载函数,每个函数都有不同数量和类型的Wrapper类作为其参数。
Is there a way to use variadic templates to call template method f with a variable number of arguments, passing in the same number of arguments but instead with their wrapper classes into function g?
有没有办法使用可变参数模板调用带有可变数量参数的模板方法f,传入相同数量的参数,而不是将它们的包装类传入函数g?
Any help much appreciated, Tony
任何帮助非常感谢,托尼
1 个解决方案
#1
1
The technical term is parameter packs.
技术术语是参数包。
And it should as "easy" as
它应该像“简单”一样
template<typename... T>
void f(T... args)
{
g(Wrapper<T>(args)...);
}
Of course, it requires you to have the proper g
function.
当然,它需要你有适当的g功能。
#1
1
The technical term is parameter packs.
技术术语是参数包。
And it should as "easy" as
它应该像“简单”一样
template<typename... T>
void f(T... args)
{
g(Wrapper<T>(args)...);
}
Of course, it requires you to have the proper g
function.
当然,它需要你有适当的g功能。