Re: std::tr1::array iterator types
On 2008-04-07 13:00:57 -0400, "Bo Persson" <bop@gmb.dk> said:
Fokko Beekhof wrote:
Hello,
The following code compiles (gcc/linux/gnu platform)
//////////////////////
#include <tr1/array>
int main()
{
std::tr1::array<int, 7> a;
std::tr1::array<int, 6>::iterator i = a.begin()+1;
return 0;
}
/////////////////////
That it compiles is "logical" if you consider that what remains
after the first element of a an array of length 7, is an array of
length 6.
It is also "illogical" if you consider that the iterator returned by
a.begin() is of type std::tr1::array<int, 7>::iterator, and merely
adding 1 to it should cause it to change its type.
Unless, of course, all iterators of type
std::tr1::array<int, X>::iterator are the same for all X.
Question: is it indeed guaranteed in the standard that all types
std::tr1::array<int, X>::iterator are identical for all X ?
No, there are no such guarantees.
That it happens to work is probably an effect of the requirements for
iterator::value_type, which of course must be int for all
array<int,X>.
And, in particular, it's probably because that implementation uses T*
as its iterator type. If it used a nested class (to do bounds checking,
for example), then array<int,6>::iterator and array<int,7>::iterator
would be two different types.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)