如何返回变量模板的最后一种类型?

问题描述 投票:7回答:1

例如

template<typename... Ts>
LastTypeOfTs f();

如何返回变量模板的最后一种类型?

c++ c++11 variadic-templates
1个回答
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() {
  //...
}

LIVE DEMO

© www.soinside.com 2019 - 2024. All rights reserved.