如何返回可变参数模板的最后一种类型?

use*_*020 7 c++ variadic-templates c++11

例如

template<typename... Ts>
LastTypeOfTs f();
Run Code Online (Sandbox Code Playgroud)

如何返回最后一种可变参数模板?

101*_*010 9

你可以做一个模板递归,如下所示:

template<typename T, typename... Ts>
struct LastTypeOfTs {
   typedef typename LastTypeOfTs<Ts...>::type type;
};

template<typename T>
struct LastTypeOfTs<T> {
  typedef T type;
};

template<typename... Ts>
typename LastTypeOfTs<Ts...>::type f() {
  //...
}
Run Code Online (Sandbox Code Playgroud)

现场演示