如何返回最后一种类型的可变参数模板? [英] how to return the last type of a variadic template?

查看:144
本文介绍了如何返回最后一种类型的可变参数模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

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

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

  template<类型名T,类型名... 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


For example

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

How to return the last type of a variadic template?

解决方案

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

这篇关于如何返回最后一种类型的可变参数模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆