How does C ++ start and end when the argument is an array?

advertisements

This question already has an answer here:

  • How does std::end know the end of an array? 3 answers

A problem in C++ primer, when begin and end work on vector I know there is vector::size() could help, but how do they work when I just give an array argument. just like:

int arr[] = {1, 2, 3};
size = end(arr) - begin(arr);

how do end(arr) and begin(arr) work correctly ?


So to see how std::end works we can look at How does std::end know the end of an array? and see that the signature for std::end is:

template< class T, std::size_t N >
T* end( T (&array)[N] );

and it is using template non-type parameter to deduce the size of the array and it is just a matter of pointer arithmetic to obtain the end:

return array + N ;

For std::begin the signature is identical, with the exception of the name:

template< class T, std::size_t N >
T* begin( T (&array)[N] );

and calculating the beginning of the array is simply a matter of array to pointer decay which gives us a pointer to the first element of the array.

In C++14 these both become constexpr.