Variadic template parameters of one specific type

3.4k views Asked by At

Why there is no specific type allowed in a variadic template pack?

template< typename T >
class Foo
{
public:
    template< typename... Values >
    void bar( Values... values )
    {
    }

    template< T... values >            <-- syntax error
    void bar( T... values )
    {
    }

    template< int... values >          <-- syntax error
    void bar( int... values )
    {
    }
};

Whats the rationale in not allowing this?
Are there proposals for this?


Note: alternatives would be

  • std::initializer_list< T > without narrowing of types and the { }-brace-syntax
  • a (ugly) recursive trait that checks all types seperately: see here
1

There are 1 answers

0
jrok On

It IS allowed, actually, you're just using it wrong. T... and int... are non-type parameter packs and their elements are values, so you can't use them as type specifiers (and you can't deduce them from a function call).

An example of correct usage:

template<int... Is>
struct IntPack {};

IntPack<1,2,3> p;

or

template< typename T >
struct Foo
{
    template< T... Ts>
    void bar()
    {
    }
};

int main()
{
    Foo<int> f;
    f.bar<1,2,3>();
}

Another example would be std::integer_sequence.