I’ve made a container class in C++, and I have a constructor from iterators so I can write MyContainer<double> x(v.begin(), v.end()) where v is a std::vector<double>. I would like to be able to do the same with a c-array but :
double array[3] = {1., 2. , 3.};
MyContainer<double> x(array, array+3); // Doesn't work : no matching function for call to ‘MyContainer<double>::MyContainer(double [3], double*)’
MyContainer<double> x(array+0, array+3); // Work
What is the origin of the problem and how to solve it ?
Thank you very much.
Don’t accept references to iterators, take them by value. It’s trying to pass a reference to an array; the failing expression needs the array to decay to a pointer.
Presumably you have
but you need
Iterators need to be lightweight enough to pass by value; all the standard templates do so.
An array cannot be used as an iterator because it cannot be incremented. The storage is fixed. When you use an array in an expression like
arr + 0or pass it by value to a function, it is implicitly converted to a pointer to its first element. But that conversion doesn’t happen when passing by reference.