how to return the last type of a variadic template?

狂风中的少年 提交于 2020-01-14 07:54:13

问题


For example

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

How to return the last type of a variadic template?


回答1:


You could do a template recursion as below:

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



来源:https://stackoverflow.com/questions/26088683/how-to-return-the-last-type-of-a-variadic-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!