Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract variadic template parameter pack and use it in another variadic template in a type traits meta-function?

I want to determine if any variadic class template is the base of another class. Typically I'd use std::is_base_of, but I don't think my use case fits, and I'm not sure if there's already something in std or boost to handle this. I want the variadic base class template's parameter pack to come from another variadic class template. Here's some example code that hopefully explains what I want to do:

Usage:

is_variadic_base_of<
   VarClassTemplA
   , ClassDerivedFromA
   , VarClassTemplB //Has param pack I want to use with ClassA
>::value;

Guts:

//test for variadic base of non-variadic
template <template<typename...> class A, typename B, typename... ArgsC>
struct is_variadic_base_of
: std::is_base_of<A<ArgsC...>, B>
{};

Is this possible?

like image 634
Brett Rossier Avatar asked May 20 '11 20:05

Brett Rossier


2 Answers

template <template<typename...> class A, typename B, typename ArgsC>
struct is_variadic_base_of;

template <template<typename...> class A, typename B, 
          template<typename...> class C, typename ...ArgsC>
struct is_variadic_base_of<A, B, C<ArgsC...>> 
: std::is_base_of<A<ArgsC...>, B>
{};

Hope it helps!

like image 52
Johannes Schaub - litb Avatar answered Nov 13 '22 16:11

Johannes Schaub - litb


You're nearly there, but try it with a partial specialization:

template<
    template<class...> class A, class B, class C
>
struct is_variadic_base_of;

// partial spec
template<
    template<class...> class A, class B,
    template<class...> class C, class... ArgsC
>
struct is_variadic_base_of< A,B,C<ArgsC...> >
  : std::is_base_of< A<ArgsC...>,B >
{};
like image 43
Xeo Avatar answered Nov 13 '22 17:11

Xeo