Say I have a class with an integer template argument. How would I go about adding a function that takes N
arguments of a given type? If at all possible, I would like to avoid enable_if
and the likes.
What I currently do is:
template<unsigned int N>
class Foo
{
template <typename To, typename>
using to = To;
public:
template<typename... Args>
void Bar(to<char, Args>... args) // Some arguments of type char
{ /* do stuff */ }
};
This allows for calling Bar:
Foo<3> myfoo;
myfoo.Bar<int, int, int>('a', 'b', 'c');
However, I see two downsides. First, the number of arguments is not necessarily limited to N
, except by maybe code inside Bar
.
Second, the user is required to add template parameters to the function. Their values are not actually used, only the number of arguments. This seems unnecessary as we already have this, namely N
.
Optimal use of the function would look like this:
Foo<3> myfoo;
myfoo.Bar('a', 'b', 'c'); // valid
The following calls generate compile errors. function does not take this many arguments
, or some similar standard compile error which you would get as if you would define void(char, char, char)
by hand.
myfoo.Bar('a', 'b');
myfoo.Bar('a', 'b', 'c', 'd'); // error
myfoo.Bar('a', 'b', "flob"); // error
// ...