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-syntaxVariadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.
Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue.
A variadic template is a class or function template that supports an arbitrary number of arguments. This mechanism is especially useful to C++ library developers: You can apply it to both class templates and function templates, and thereby provide a wide range of type-safe and non-trivial functionality and flexibility.
Parameter packs (C++11) A parameter pack can be a type of parameter for templates. Unlike previous parameters, which can only bind to a single argument, a parameter pack can pack multiple parameters into a single parameter by placing an ellipsis to the left of the parameter name.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With