Вопрос

For example I have a code as below with two parameter packs

template<class... Ts, int... Is>
struct B
{

};

int main()
{
    B<int, double, 0, 1> b; // compile error here
    return 0;
}

Any way to get is right?

Это было полезно?

Решение

That is not allowed. You can do this, however:

template<typename ...> struct typelist {};

template<typename TypeList, int... Is>
struct B;                                //primary template. Only declaration!

template<typename ... Ts, int ... Is>
struct B<typelist<Ts...>, Is...>         //partial specialization
{
    //here you know Ts... and Is... Use them!
};

int main()
{
    B<typelist<int, double>, 0, 1> b; 
    return 0;
}

Другие советы

You might introduce helper templates to introduce lists of types / integers:

template<typename TList, typename IList> struct X;

template<
    template<typename ...> class TList, typename ...Ts,
    template<int ...> class IList, int ...Is>
struct X <TList<Ts...>, IList<Is...> >
{

};

template<typename ...Ts> struct TList {};
template<int ...Is> struct IList  {};

int main()
{
    X<TList<int, double>, IList<0, 1> > x;
    return 0;
}

I think this suffers from the XY problem. If I have understood well, what you want is to pass any number of numbers of different types.

You could do that using std::integral_constant:

template<typename... INTS>
struct foo
{
    ...
};

template<typename T , T n>
using number = std::integral_constant<T,n>; //An alias for less typing...

foo<number<int,-1>,number<unsigned int,1>, /* etc */>;

If thats not what you want, I would use the typelist partial-specialization approach others suggested.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top