Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ Function that accepts array.begin() and array.end() as arguments

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;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As far as I understand, the begin/end functions return a pointer to the first/last element of the array

No. begin and end return iterators. The standard library works with iterators. Iterators are a generalization of pointers.

Iterators behave like pointers and you use them like pointers (e.g. *it to access the element), with some caveats: not all iterators have all the operations a pointer does. A pointer satisfies the random access iterator concept, so on some implementations the iterator of std::array could be just an alias for the pointer type, but you can’t rely on that. E.g. on the same compiler it can be a pointer for the release build, but a full class for the debug build.

The idiomatic way is to write:

template<class It>
void foo(It begin, It end) {
     for (auto it = begin; it != end; ++it) {
          const auto& elem = *it;
          // ..
     }
}

Since C++20 we should transition from iterator pairs to ranges:

void foo(std::ranges::range const auto& r) {
    for (const auto& elem : r) { 
        // ...
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading