【问题标题】:how to return the last type of a variadic template?如何返回可变参数模板的最后一种类型?
【发布时间】:2014-09-28 19:21:10
【问题描述】:

例如

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

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

【问题讨论】:

标签: c++ c++11 variadic-templates


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2023-02-05
    • 2019-09-01
    相关资源
    最近更新 更多