Given this how does the end() function added in C++ 11 know the end of the array.
1 Answer
I'm not sure I understand your question... it could be implemented like this maybe?
namespace std
{
template<class T, size_t N>
T *end(T (&arr)[N]) { return &arr[N]; }
}
13 Comments
Jerry Coffin
@Himanshu: The size of the array is known at compile time. The template basically extracts that information that the compiler already knew.
user541686
@user2485710: That is correct but how is it relevant to the question?
Xeo
IIRC, in C++ this actually invokes UB due to dereferencing the past-the-end pointer of the array (inside the subscript). The safe way would be
&arr[0] + N.user541686
@user2485710: In all honestly I'm not understanding what you're trying to say. Maybe Jerry does?
R. Martinho Fernandes
@Mehrdad you're the one that has to provide a reference. The standard prefers to state guarantees, not non-guarantees. Asking someone to provide a standard reference for a non-guarantee is dishonest.
|