I want my function to be able to take array.begin() and array.end() as arguments. As far as I understand, the begin/end functions return a pointer to the first/last element of the array. Then why does the following code not work? How should I write the code instead?
#include <iostream>
#include <array>
template<class T> void foo(const T* begin, const T* end) {
...
}
int main() {
std::array<int, 5> arr = { 1, 2, 3, 4, 5 };
foo(arr.begin(), arr.end()); // <-- error!
return 0;
}
begin/endreturn iterators which behave like pointers (and may even be implemented as pointers), but are not necessarily pointers themselves. Your code woks fine in GCC and Clang, since their standard library implementations both implement std::array's iterators as pointers, but it doesn't work under MSVC, which defines a separate iterator class for std::array.beginandendreturn iterators which in some standard libraries might just be a pointer but there is no guarantee.beginand.endmethods, which will show you what they actually return.