繁体   English   中英

有没有办法将模板的参数列表放入另一个元函数中?

[英]Is there a way to pull out a template's parameter list to put into another metafunction?

说我有一个get_at元函数,它从可变参数模板参数列表中获取第N个类型:

template <int I, typename ...Ts>
struct get_at;

template <int I, typename T, typename ...Ts>
struct get_at<I, T, Ts...> : public get_at<I-1, Ts...>
{
};

template <typename T, typename ...Ts>
struct get_at<0, T, Ts...> : public std::true_type
{
  typedef T type;
};

template <int I>
struct get_at<I> : public std::false_type
{
};

我想将Ts...存储在这样的列表类型中:

template <typename... Ts>
struct list {};

我如何从包含list类型中获取Ts... ,以便可以将其放入get_at元函数中?

不用了 只需编写另一个元函数(可以通过另一层间接解决任何问题?):

template <int I, typename Seq>
struct get_at_seq;

template <int I, typename... Ts>
struct get_at_seq<I, list<Ts...>>
: get_at<I, Ts...>
{ };

甚至:

template <int I, template <typename...> class C, typename... Ts>
struct get_at_seq<I, C<Ts...>>
: get_at<I, Ts...>
{ };

不过要注意的是,从true_typefalse_type继承对于此用例没有意义。 您可以覆盖type ,但是在get_at上仍然有operator bool() ,这种用法毫无意义。 最好只是显式地写入value ,例如:

template <typename T, typename ...Ts>
struct get_at<0, T, Ts...>
{
    typedef T type;
    static constexpr bool value = true;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM