Let N
be a template parameter of type std::size_t
. I would like to be able to call the constructor for my class in two ways:
A a(x1, x2, x3, ..., xN)
and
A a(x1, x2, x3, ..., xN, xN1)
where the xi
variables are all of the same type. My first thought was to do:
template <std::size_t N>
struct A
{
template <typename ...Args, typename = typename std::enable_if<N == sizeof...(Args), void>::type>
A(Args ...args) {
f(args...); // where f is some function
}
template <typename ...Args, typename = typename std::enable_if<N+1 == sizeof...(Args), void>::type>
A(Args ...args) {
// run f on the first N arguments
// run g on the last argument (selection is done using templates, I just did not want to write the code)
}
};
This technique is explained at: Variadic templates with exactly n parameters. Of course, the problem with this is that you cannot overload the constructor this way.
Any ideas?