I am writing a storage container class template that wraps private std::arrayto add some features to it. The template parameterizes the number of values as follows:
template<size_t N> class Vector {
private:
array<double, N> vals;
public:
[...]
};
I would like the constructor for the class to accept only Ndoubles to fill the array, but I cannot find a good way to do this. The variadic arguments do not provide a mechanism for checking how many of them are, so they are right. The parameter packages do not perform floating point promotion, but I would like to deal with this if I could only figure out how to use them for this.
I tried to follow the approach in the answer to the participant function template with the number of parameters depending on the integral parameter of the template , but I can not understand the meaning enable_if<>::type=0. I tried to naively copy this code (although I would rather understand how it works. I saw people use it ::valuein other places, but I can’t find the documentation about why), but expanding the resulting parameter package does not seem to work. My other problem with parameter packages is that I'm not sure if they guarantee that the types of all arguments were the same.
I tried to run static_assertin the size of the initializer list, in the constructor body, but of course, the size of the list is not constant at compile time, so this also does not work.
Is there a standard approach here? Am I just using parameter packages incorrectly?
Update:
I have an approach in the answer that I linked above, partially working:template<size_t N> class Vector {
private:
array<double, N> vals;
public:
template <typename ...T,
typename enable_if<sizeof...(T) == N, int>::type = 0>
Vector(T ...args) {
vals = {args...};
}
};
The problem is that the term enable_ifin the template means that when I initialize Vector, for example, with
Vector<3> V {1.0, 2.0, 3.0};
He requests specialization of a template Vector<3>::Vector<double, double, double, 0>, but not <double, double, double>. How can I get rid of this wandering term in a template?