std::tr1::array iterator types

Discussion in 'C++' started by Fokko Beekhof, Apr 7, 2008.

  1. 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
    Fokko Beekhof, Apr 7, 2008
    #1
    1. Advertising

  2. Fokko Beekhof

    Bo Persson Guest

    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>.


    Bo Persson
    Bo Persson, Apr 7, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Gottman

    std::set versus tr1::unordered_set

    Joe Gottman, Jun 3, 2005, in forum: C++
    Replies:
    1
    Views:
    7,135
    Howard Hinnant
    Jun 4, 2005
  2. Replies:
    6
    Views:
    628
    Jim Langston
    Oct 30, 2005
  3. Rares Vernica

    error with tr1 unordered_map iterator

    Rares Vernica, Feb 24, 2007, in forum: C++
    Replies:
    6
    Views:
    1,787
  4. Emmanuel Deloget
    Replies:
    3
    Views:
    392
    Chris Thomasson
    Mar 3, 2007
  5. Sarath
    Replies:
    2
    Views:
    591
    Knockr
    Aug 19, 2009
Loading...

Share This Page