Variational template parameters of one specific type

Why is there no specific type in the variation package?

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

What justification does not allow this?
Are there any suggestions for this?


Note: alternatives would be

  • std::initializer_list< T > without constriction and syntax { } -brace
  • a (ugly) recursive trait that checks all types separately: see here
+6
c ++ language-lawyer c ++ 11 templates variadic-templates
source share
1 answer

It is allowed, in fact, you just use it incorrectly. T... and int... are unpaired parameter packages, and their elements are values, so you cannot use them as type specifiers (and you cannot deduce them from a function call).

An example of proper use:

 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 .

+6
source share

All Articles