如何实现可变参数模式以将可变数量的参数转发给 C++11 中的函数?

How do I implement a variadic pattern to forward variable number of arguments to a function in C++11?

在 python 中,我可以使用 *args 允许函数输入的数量可变。例如,以下代码片段将打印出调用 f 时传递的所有参数:

def f(*args):
  for a in args:
    print(a)

我希望能够在满足以下要求的 C++11 中实现这样的模式:

函数 f 将始终接受特定类型 T 的值,然后是可变数量的输入;这可能包括 0 个额外输入。

额外的输入不一定是同一类型,因此使用初始化列表是行不通的。

函数 f 将被另一个函数 g 调用,该函数需要将可选参数转发给 f:

T g(const T& x, args...) {
  T output = f(x, args...);
  return output;
};

T f(const T& x, args...) {
  // do some stuff and return an object of type T
};

如何解决这个设计问题?我已经尝试过可变参数模板,但我似乎无法使我的实现正常工作(编译但不 link 因为右值引用问题)。

以下是用 C++ 编写的方法:

template <class... A>
T f(const T &x, A &&... arg)
{
  // do some stuff and return an object of type T
}

template <class... A>
T g(const T &x, A &&... arg)
{
  T output = f(x, std::forward<A>(arg)...);
  return output;
}

请注意,由于涉及模板,代码必须in a header file以防止链接问题。