Brett Rossier
Brett Rossier

Reputation: 3482

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?

Upvotes: 5

Views: 1496

Answers (2)

Xeo
Xeo

Reputation: 131799

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

Upvotes: 4

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507005

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!

Upvotes: 6

Related Questions