Topic: 24.1 [lib.iterator.requirements] Defect Report


Author: craig.henderson@orchestria.com (Craig Henderson)
Date: Fri, 17 Jan 2003 19:07:55 +0000 (UTC)
Raw View
Section 24.1 Para 6 states
"...If j is reachable from i, they refer to the same container."

In my view, this is fundamentally flawed, and cannot be relied upon as
a property of any iterator. Containers generally allocate storage from
the heap. The heap is a contignuous memory range, often per-thread,
and so an iterator in two containers are reachable (from the earlier
definition in para 6).

Consider the trivial example below. This program reports that ite2 is
reachable from itb1, however, they refer to different containers.  Is
this a defect, as I believe it is? If not, why? If so, is it known? I
could not find reference to this on the C++ Standard Library Active
Issues List at http://anubis.dkuug.dk/JTC1/SC22/WG21/docs/lwg-active.html

Regards
-- Craig


#include <vector>
#include <iostream>

typedef std::vector<int> vector_t;

int main()
{
    vector_t vec1, vec2;
    vec1.push_back(1);
    vec2.push_back(2);

    vector_t::const_iterator itb1 = vec1.begin();
    vector_t::const_iterator ite1 = vec1.end();
    vector_t::const_iterator itb2 = vec2.begin();
    vector_t::const_iterator ite2 = vec2.end();
    std::cout << "Vector 1: " << itb1 << " " << ite1 << std::endl;
    std::cout << "Vector 2: " << itb2 << " " << ite2 << std::endl;

    const int dist = std::distance<>(itb1, ite2);
    std::cout << "Distance from vec1.begin() to vec2.end() is "
              << dist << std::endl
              << "That's " << dist*sizeof(*itb1) << " bytes"
              << std::endl;

    for (int loop=0; loop<dist; ++loop)
        ++itb1;
    if (itb1 == ite2)
        std::cout << "ite2 is reachable from itb1";
    else
        std::cout << "ite2 is NOT reachable from itb1";

    return 0;
}


Produces output:
Vector 1: 002F3248 002F324C
Vector 2: 002F3260 002F3264
Distance from vec1.begin() to vec2.end() is 7
That's 28 bytes
ite2 is reachable from itb1

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Sat, 18 Jan 2003 02:24:12 +0000 (UTC)
Raw View
Craig Henderson wrote:
> Consider the trivial example below. This program reports that ite2 is
> reachable from itb1, however, they refer to different containers.  Is
> this a defect, as I believe it is? If not, why? If so, is it known?

Table 72 in 24.1.1 states that a pre-condition for applying
operator++ to an iterator is that it must be dereferenceable.
Your code applies it to past-the-end iterators, which are not.
It's your code which is in error, not the Standard.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]