The following code compiles fine on my system:
#include <array>
#include <type_traits>
static_assert(std::is_same<std::array<int, 5>::iterator,
std::array<int, 7>::iterator>::value, ":(");
Is that behavior guaranteed by the standard? Is the iterator type independent of the array size?
If it is guaranteed, is there any way to abstract from the element type and ignore the size?
template<typename T, size_t n>
void foobar(std::array<T, n>::iterator it)
That is, is there any way to write the above array-specific code without mentioning the size n
?
Note that I do not want to resort to T*
, even though in release mode the iterator probably is a T*
.
No, there are no guarantees. The standard just says
typedef implementation-defined iterator;
The iterator type could be a plain pointer, a class that is a member of the array
, or a separate class wrapping the plain pointer.
If it is a member class, it will depend on the array size. Otherwise likely not.
No, it's not guaranteed. Each array type array<T, size_t>
has a nested member typedef named iterator
whose type is implementation defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With