Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return the last type of a variadic template?

For example

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

How to return the last type of a variadic template?

like image 809
user1899020 Avatar asked Sep 28 '14 19:09

user1899020


1 Answers

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

like image 81
101010 Avatar answered Nov 12 '22 16:11

101010