wimalopaan
wimalopaan

Reputation: 5472

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

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>;
};

Upvotes: 3

Views: 101

Answers (1)

Vittorio Romeo
Vittorio Romeo

Reputation: 93384

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

Upvotes: 3

Related Questions