7

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 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.