Meta-Function to get the first template of a list of templates

Deadly 提交于 2019-12-23 19:00:54

问题


Using the following metafunction front to get the first type of a typelist I try to write a similar metafunction to extract the first template of a list of templates.

namescpace detail {
    template<typename L> struct front_impl;
    template<template<typename, typename...> typename L, typename F, typename... I>
    struct front_impl<L<F, I...>> {
        typedef F type;  
    };
}
template<typename L> struct front_impl;
template<template<typename, typename...> typename L, typename F, typename... I>
struct front_impl<L<F, I...>> {
    typedef F type;  
};

One can use this as follows:

template<typename... T>
struct List {
    inline static constexpr size_t size = sizeof...(T);
};

using l1 = List<A, B, C>;
using f1 = front<l1>;

Now I try to do the same with a list of templates. Therefore I use a list of templates TList:

template<template<typename> typename... TT>
struct TList {
    inline static constexpr size_t size = sizeof...(TT);
};

and the metafunction tfront:

template<typename T> struct tfront;
template<template<typename> typename F, template<typename> typename... R>
struct tfront<TList<F, R...>> {
    template<typename T> using type = F<T>;
};

Then I can extract the first of a list of templates A, B, ... (not shown here):

    using tlist = TList<A, B, C>;

    template<typename T>
    using f = typename tfront<tlist>::template type<T>;

    f<int> xx;

Then xx ist of type A<int>.

The Question is: can I write the metafunction tfront in the same way as front, that is not as a partial specialization for the list of templates TList but for every variadic template of templates? So I would like to introduce a parameter L for tfront as a template-template-parameter with a variadic list of templates, so that the compiler must also deduce the type of the type L as in the case of front.

I would like to write something like (but what to use as ???):

template<template<typename> typename F, ??? TL, template<typename> typename... R>
struct tfront<TL<F, R...>> {
    template<typename T> using type = F<T>;
};

回答1:


You need an extra layer of template<typename> typename...:

template<typename>
struct tfront;

template<template<typename> typename F, 
         template<template<typename> typename...> typename TL, 
         template<typename> typename... R>
struct tfront<TL<F, R...>> 
{
};

live example on wandbox



来源:https://stackoverflow.com/questions/47775990/meta-function-to-get-the-first-template-of-a-list-of-templates

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