std::tr1::array iterator types

F

Fokko Beekhof

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 ?

Thanks in advance,
Fokko Beekhof
 
B

Bo Persson

Fokko said:
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>.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top